程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> jdbc-幫幫忙,看看應該改哪裡,謝謝

jdbc-幫幫忙,看看應該改哪裡,謝謝

編輯:編程綜合問答
幫幫忙,看看應該改哪裡,謝謝

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)

最佳回答:


 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. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved