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



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

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