| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 733 人关注过本帖
标题:请问一个关于数据库连接的问题
只看楼主 加入收藏
huxiangwenhu
Rank: 4
等 级:业余侠客
威 望:1
帖 子:191
专家分:262
注 册:2010-6-1
结帖率:90.91%
收藏
已结贴  问题点数:20 回复次数:5 
请问一个关于数据库连接的问题
代码如下:
package fouth;

public class InterfaceTest {
   
    public static void main(String[] args) {
        Student astudent=new Student("tom", "123456");
        astudent.setAddress("beijing");
        astudent.setSex("men");
        System.out.println(astudent);
    }
}
interface Person{
    String getName();
    String getSex();
    String getBirthday();
    String getAddress();
    int setAddress(String strAddress);   
}
class Student implements Person,Comparable{
    private String strName="";
    private String strSex="";
    private String strNumber="";
    private String strBirthday="";
    private String strSpeciality="";
    private String strAddress="";
    public String getSex() {
        return strSex;
    }
    public void setSex(String strSex) {
        this.strSex = strSex;
    }
    public String getBirthday() {
        return strBirthday;
    }
    public void setBirthday(String strBirthday) {
        this.strBirthday = strBirthday;
    }
    public String getSpeciality() {
        return strSpeciality;
    }
    public void setSpeciality(String strSpeciality) {
        this.strSpeciality = strSpeciality;
    }
    public String getAddress() {
        return strAddress;
    }
   
    public String getName() {
        return strName;
    }
    public String getNumber() {
        return strNumber;
    }
    public Student(String strName, String strNumber) {
        super();
        this.strName = strName;
        this.strNumber = strNumber;
    }
    public String toString(){
        String information="学生姓名="+strName+",学号="+strNumber;
        if(!strSex.equals("")){
            information+=",性别="+strSex;
        }
        if(!strBirthday.equals("")){
            information+=",出生年月="+strBirthday;
        }
        if(!strSpeciality.equals("")){
            information+=",专业="+strSpeciality;
        }
        if(!strAddress.equals("")){
            information+=",籍贯="+strAddress;
        }
        return information;   
    }
    public int setAddress(String strAddress) {
        this.strAddress=strAddress;
        return 0;
    }   
    public String getStrAddress() {
        return strAddress;
    }
    @Override
    public int compareTo(Object o) {
        // TODO Auto-generated method stub
        return 0;
    }
   
}


报错如下
java.sql.SQLException: The url cannot be null
    at java.sql.DriverManager.getConnection(DriverManager.java:554)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at eight.DoUpdateTest.getStudent(DoUpdateTest.java:23)
    at eight.DoUpdateTest.main(DoUpdateTest.java:19)

能创建表,但是不能输出表
请问这是为什么,地址不存在为空啊!
搜索更多相关主题的帖子: 数据库 
2010-08-30 16:35
shellingford
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:19
帖 子:228
专家分:1348
注 册:2010-8-9
收藏
得分:6 
连接数据库的代码呢?贴上来看看
2010-08-30 17:43
linjx0123
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
收藏
得分:6 
你贴的代码跟你出现的错误一点都不相关。
根据你的报错,是数据库连接的时候url为空。
2010-08-30 18:13
huxiangwenhu
Rank: 4
等 级:业余侠客
威 望:1
帖 子:191
专家分:262
注 册:2010-6-1
收藏
得分:0 
回复 3楼 linjx0123
//真的对不起,贴错代码了,应该是这个:

package eight;

import
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Properties;

public class DoUpdateTest {
    private Connection con;
    private String url;
    private String userName;
    private String passWord;
    public static void main(String[] args) {
        DoUpdateTest test=new DoUpdateTest();
        test.getProperty();
        Connection con=test.getConnection();
        test.createTable(con);
        test.getStudent(con);
    }
    private void getStudent(Connection con) {
        try{
            con=DriverManager.getConnection(url,userName,passWord);
        }catch(Exception ex){
            ex.printStackTrace();
            }
        }
    private void createTable(Connection con) {
        try{
            Statement st=con.createStatement();
            String sql="CREATE TABLE STUDENT(姓名 varchar(12) NOT NULL,"+
            "学号  VARCHAR(10) NOT NULL,"+"出生日期 DATETIME NOT NULL,专业 VARCHAR(10) NULL,"+
            "籍贯 VARCHAR(30) NULL)";
            System.out.println("输出的SQL语句是:");
            System.out.println(sql);
            st.execute(sql);
            sql="insert into student values('王成','20021022','1977-6-7','英语','河北')";
            st.executeUpdate(sql);
            st.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }
        
    }
    private void getProperty() {
        Properties prop =new Properties();
        try{
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            con=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pubs","sa","");
            }catch(Exception ex){
                ex.printStackTrace();
            }
    }
    public Connection getConnection(){
        try{
            con=DriverManager.getConnection(url,userName,passWord);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return con;
    }

}
   
        

麻烦帮我看下啊,谢谢了!
2010-08-30 20:10
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
收藏
得分:6 
url初始为null
你可以在声明的时候赋值
private String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pubs";
private String userName = "sa";
private String passWord = "";

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2010-08-31 08:55
linjx0123
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:279
专家分:1362
注 册:2006-4-7
收藏
得分:0 
private void getProperty() {
        Properties prop =new Properties();
        try{
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pubs";
            userName = "sa";
            passWord = "";
            }catch(Exception ex){
                ex.printStackTrace();
            }
    }
2010-08-31 10:59
快速回复:请问一个关于数据库连接的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021155 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved