2016-06-29 5 views
0

次のコードを使用してSQL Serverテーブルにユーザーが入力したデータを挿入しようとしましたが、コードはエラーなしで実行されますが、データは挿入されません。netbeansのjtextfieldを使用してSQL Serverテーブルにデータを挿入

try { 
    Class.forName(driver); 
    Connection con=DriverManager.getConnection(url, user, pass); 
    String sql="insert into inventory" 
       +"(Product_Code,Product_Name,Quantity,Cost)" 
       +"value(?,?,?,?)"; 
    PreparedStatement pst=con.prepareStatement(sql); 
    pst.setString(1, product_code.getText()); 
    pst.setString(2, product_name.getText()); 
    pst.setString(3, quantity.getText()); 
    pst.setString(4, price.getText()); 
    pst.executeUpdate(); 
    JOptionPane.showMessageDialog(this, "entry successful");  
}          
catch(ClassNotFoundException | SQLException | HeadlessException e){ 
    JOptionPane.showMessageDialog(this, "entry successful"); 
} 
+0

'Quantity、Cost' - >' getText() '?整数/浮動小数点数に変換しようとする – gofr1

+0

まだ行を挿入できない –

+0

そのパラメータで正確に何を渡しますか?データのサンプルを表示できますか? – gofr1

答えて

0

INSERT以下のスクリプトの構文エラー:私はあなたを示唆して論評をもとに

String sql="insert into inventory" 
     +" (Product_Code, Product_Name, Quantity, Cost)" 
     +" values (?, ?, ?, ?)"; 
+0

テーブルに値をまだ挿入できません –

0

:あなたのコードがされるので、それは、values(あるべきvalue(

String sql="insert into inventory" 
      +"(Product_Code,Product_Name,Quantity,Cost)" 
      +"value(?,?,?,?)"; 

誤植このコードを試す:

String url = "jdbc:sqlserver://localhost:1433;databaseName=YourDBName;integratedSecurity=false;user=MyUserName;password=*****;"; 

try { 
    Class.forName(driver); 
    Connection con=DriverManager.getConnection(url); 

    if (con!= null) { 
     System.out.println("Connected"); 
    } 

    String sql="insert into inventory" 
       +" (Product_Code,Product_Name,Quantity,Cost)" 
       +" values (?,?,?,?)"; 
    PreparedStatement pst=con.prepareStatement(sql); 
    pst.setString(1, product_code.getText()); 
    pst.setString(2, product_name.getText()); 
    pst.setInt(3, Integer.parseInt(quantity.getText())); 
    pst.setFloat(4, Float.parseFloat(price.getText())); 

    int rowsInserted = pst.executeUpdate(); 
    if (rowsInserted > 0) { 
     System.out.println("A new row was inserted successfully!"); 
    } 

    JOptionPane.showMessageDialog(this, "entry successful");  
}          
catch(ClassNotFoundException | SQLException | HeadlessException e){ 
    JOptionPane.showMessageDialog(this, "entry successful"); 
} 
+0

エラー "参照可能なNULLポインタ"が "PreparedStatement pst = con.prepareStatement(sql);"行に表示されています –

+0

接続が確立しましたか?あなたは '接続された'メッセージを得ましたか? – gofr1

+0

そして私は 'url'を変更して、DB名のユーザーとパスワードを変更する必要があります。 – gofr1

関連する問題