sinat_25945001 2015-08-17 23:42 采纳率: 100%
浏览 1308
已采纳

帮帮忙,看看应该改哪里,谢谢

package javaJDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/*

  • //采用PreparedStatement添加数据
    */
    public class InsertTest02 {

    public static void main(String[] args) {
    Connection conn = null;
    PreparedStatement pstmt = null;
    try{
    //加载数据库驱动
    Class.forName("com.mysql.jdbc.Driver");
    //连接数据库
    String dbUrl = "jdbc:mysql://localhost:3306/student";
    String username = "root";
    String password = "mysql";
    //执行PreparedStatement语句,执行SQL
    String name = "莫燕";
    String number = "123456";
    String class1 = "1102";
    int score = 97;
    String sql = "insert into stu(name, number, class1, scoree) values (?, ?, ?, ?)";
    pstmt = conn.prepareStatement(sql);
    pstmt.setString(1, name);
    pstmt.setString(2, number);
    pstmt.setString(3, class1);
    pstmt.setInt(4, score);
    pstmt.executeUpdate();
    System.out.println("添加员工成功");
    }catch(ClassNotFoundException e){
    e.printStackTrace();
    }catch(SQLException e){
    e.printStackTrace();
    }finally{
    try{
    //关闭原则:从里到外
    if(pstmt != null)
    pstmt.close();
    if(conn != null)
    conn.close();
    }catch(SQLException e){
    e.printStackTrace();
    }

    }
    

    }

}
Exception in thread "main" java.lang.NullPointerException
at javaJDBC.InsertTest02.main(InsertTest02.java:29)

  • 写回答

2条回答

  • 丵鹰 2015-08-18 01:25
    关注
     public static void main(String[] args) {
             Connection conn = null;
             PreparedStatement pstmt = null;
             try{
             //加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
             //连接数据库
            String dbUrl = "jdbc:mysql://localhost:3306/student";
             String username = "root";
             String password = "mysql";
             //执行PreparedStatement语句,执行SQL
             String name = "莫燕";
            String number = "123456";
             String class1 = "1102";
             int score = 97;
             String sql = "insert into stu(name, number, class1, scoree) values (?, ?, ?, ?)";
             conn= DriverManager.getConnection(dbUrl, username, password);   ------少了这一句
             pstmt =conn.prepareStatement(sql);
             pstmt.setString(1, name);
             pstmt.setString(2, number);
             pstmt.setString(3, class1);
             pstmt.setInt(4, score);
             pstmt.executeUpdate();
             System.out.println("添加员工成功");
             }catch(ClassNotFoundException e){
             e.printStackTrace();
             }catch(SQLException e){
             e.printStackTrace();
             }finally{
             try{
             //关闭原则:从里到外
            if(pstmt != null)
             pstmt.close();
             if(conn != null)
             conn.close();
             }catch(SQLException e){
             e.printStackTrace();
             }
            }
    
    
            }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog