开发者社区> 问答> 正文

jdbc连接数据库的问题

蛮大人123 2016-06-14 15:01:42 1126

我是根据教程利用odbc做驱动通过jdbc连接sqlserver2008数据库的,数据表已经建立好,odbc加载驱动是成功的,数据库也连接上了,编写的java代码也没有报错,但是就是不能正常运行添加数据,提示是空异常,纠结了好久没能解决,所以在此拜托各位帮个忙解决一下。在此,表示感谢。以下是代码:
用户类

public class User {
private int id;
private String userName;
private String passWord;
private int roleId;
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassWord() {
    return passWord;
}

public void setPassWord(String passWord) {
    this.passWord = passWord;
}

public int getRoleId() {
    return roleId;
}

public void setRoleId(int roleId) {
    this.roleId = roleId;
}

public User(){

}

public User(int id, String userName, String passWord, int roleId) {
    super();
    this.id = id;
    this.userName = userName;
    this.passWord = passWord;
    this.roleId = roleId;
}

@Override
public String toString() {
    return "User [id=" + id + ", userName=" + userName + ", passWord="
            + passWord + ", roleId=" + roleId + "]";
}
}

main主函数

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class demo01 {
private Connection conn;
private Statement stat;
public static void main(String[] args) {
new demo01().startup();
}
private  void startup(){
    init();
    //添加一个用户
    User user = new User(1, "zhang", "111", 1);
    addUser(user);

}
private void addUser(User user) {
    String sql = String.format("insert into T_User values('%s','%s',%s)",
            user.getUserName(), user.getPassWord(), user.getRoleId());
    try {
        stat = conn.createStatement();
        int result = stat.executeUpdate(sql);

        if (result > 0) {
            System.out.println("添加用户成功!");
        } else {
            System.out.println("添加用户失败!");
        }

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            stat.close();
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
private void init(){
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        System.out.println("加载驱动成功");
        //该语句冒号前面是固定的,冒号后面是自己定义的数据源
        String url = "jdbc:odbc:bookshopsource";        
        Connection conn = DriverManager.getConnection(url);
       System.out.println("数据库连接成功");
    } catch (ClassNotFoundException e) {
        System.out.println("加载驱动失败");
        return;
    }catch (SQLException e) {
        System.out.println("数据库连接失败");
        return;
    }
}
}

screenshot

SQL Java 数据库连接 数据库
分享到
取消 提交回答
全部回答(1)
  • 蛮大人123
    2019-07-17 19:37:45

    Connection conn = DriverManager.getConnection(url); 我觉得问题应该出现在这里。你在init方法作用域里面赋值conn,出去后conn出现了空指针,你如果是要赋值给全局变量private Connection conn就直接这么写就完了conn = DriverManager.getConnection(url);

    0 0
+ 订阅

分享数据库前沿,解构实战干货,推动数据库技术变革

推荐文章
相似问题