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 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题