2016-08-08 27 views
0

私はDBMS用にSQLiteと一緒にNetbeansを使用しています。不思議なことに、データベースにレコードを追加したり、レコードを削除したり、すべてのレコードを表示したりすることはできますが、レコードを更新しようとするとエラーは発生しません。誰が何が問題なのか知っていますか?JDBCアップデートでエラーは表示されませんが、動作しません。

import java.sql.*; 
import java.util.Scanner; 

public class Example 
{ 
    public static void main(String[] args) 
    { 
     try 
     { 
      Class.forName("org.sqlite.JDBC"); 
     } catch (Exception e) 
     { 
      System.out.println(e); 
     } 
     Connection c = null; 
     try 
     { 
      c = DriverManager.getConnection("jdbc:sqlite:Example.db"); 
     } 
     catch (SQLException s) 
     { 
      System.out.println(s); 
     } 
     String sql = "INSERT INTO Classmates VALUES (?,?,?,?,?)"; 
     String deleteSQL = "DELETE FROM Classmates WHERE cid = ?"; 
     String updateSQL = "UPDATE Classmates SET firstname=?,lastname=?,age=?,gpa=? WHERE cid =?"; 
     String showSQL = "SELECT * FROM Classmates"; 

     int cid; 
     String firstname; 
     String lastname; 
     double age; 
     double gpa; 

     char selection; 
     boolean valid 

     Scanner in = new Scanner(System.in); 
     PreparedStatement p = null; 
     ResultSet r = null; 
     do 
     { 
      System.out.print("A -Add Classmate\n"); 
      System.out.print("R - Remove Classmate\n"); 
      System.out.print("S - Show all Classmates\n"); 
      System.out.print("U - Update a Classmate\n"); 
      System.out.print("Q - Quit\n"); 

      selection = in.next().charAt(0); 

      switch(selection) 
      { 
       case 'a': 
       case 'A': 
       case 'r': 
       case 'R': 
       case 's': 
       case 'S': 
       case 'u': 
       case 'U': 
       case 'q': 
       case 'Q': valid = true; 
        break; 
       default: valid = false; 
      } 
      switch(selection) 
      { 
       case 'a': 
       case 'A': 
        System.out.print("Enter cid: "); 
        cid = in.nextInt(); 
        in.skip("\n"); 
        System.out.print("Enter first name: "); 
        firstname = in.nextLine(); 
        System.out.print("Enter last name: "); 
        lastname = in.nextLine(); 
        System.out.print("Enter age: "); 
        age = in.nextDouble(); 
        System.out.print("Enter the gpa: "); 
        gpa = in.nextDouble(); 

        try 
        { 
         p = c.prepareStatement(sql); 
         p.clearParameters(); 
         p.setInt(1, cid); 
         p.setString(2, firstname); 
         p.setString(3, lastname); 
         p.setDouble(4, age); 
         p.setDouble(5, gpa); 
         p.executeUpdate(); 
        } 
        catch (SQLException s) 
        { 
         System.out.println(s); 
        } 
        break; 
       case 'r': 
       case 'R': 
        System.out.print("Enter cid: "); 
        cid = in.nextInt(); 
        try 
        { 
         p = c.prepareStatement(deleteSQL); 
         p.setInt(1, sid); 

         // execute SQL delete 
         p.executeUpdate(); 
        } 
        catch (SQLException s) 
        { 
         System.out.println(s); 
        } 
        break; 

       case 's': 
       case 'S': 
        try 
        { 
         p = c.prepareStatement(showSQL); 
         p.clearParameters(); 
         r = p.executeQuery(); 

         while(r.next()) 
         { 
          System.out.println("CID: " + r.getInt(1) + ", First Name: " 
           + r.getString(2) + ", Last Name: " + r.getString(3) 
           + ", Age: " + r.getDouble(4) + ", GPA: " + r.getDouble(5)); 
         } 
        } 
        catch (SQLException s) 
        { 
         System.out.println("Exception 4: " + s); 
        } 
        break; 
       case 'u': 
       case 'U': 
        System.out.print("Enter cid: "); 
        cid = in.nextInt(); 
        in.skip("\n"); 
        System.out.print("Update first name: "); 
        firstname = in.nextLine(); 
        System.out.print("Update last name: "); 
        lastname = in.nextLine(); 
        System.out.print("Update age of student: "); 
        age = in.nextDouble(); 
        System.out.print("Update GPA of student: "); 
        gpa = in.nextDouble(); 
        try 
        { 
         p = c.prepareStatement(updateSQL); 
         p.clearParameters(); 
         p.setInt(1, cid); 
         p.setString(2, firstname); 
         p.setString(3, lastname); 
         p.setDouble(4, age); 
         p.setDouble(5, gpa); 
         p.executeUpdate(); 
        } 
        catch (SQLException e) 
        { 
         System.out.println(e.getMessage()); 
        } 
        break; 
       case 'q': 
       case 'Q': 
        try 
        { 
         r.close(); 
         c.close(); 
        } 
        catch(SQLException s) 
        { 
         System.out.println("Exception 5: " + s); 
        } 
        break; 
       default: 
        System.out.println("Wrong Selection"); 
      } 
     }while (selection != 'q' || selection != 'Q'); 
    } 
} 
+0

'p.setInt(1、cid);'更新クエリによれば、最初のパラメータはfname – copeg

+0

です。クエリとフィールドの位置が一致しません。 –

+2

ここでスタックオーバーフローでは、あなたを助けた答えを受け取った後、あなたの質問をゴミ箱に移動する反社会的行動とみなされます。最後の編集をロールバックしました。 –

答えて

2

あなたの更新コードはp.setInt(1, cid);持っていますが、CIDフィールドは声明の中で最後にリストされている:あなたのWHEREが失敗しているString updateSQL = "UPDATE Classmates SET fname=?,lname=?,age=?,gpa=? WHERE sid =?";

+0

これは問題でした。そのような小さな問題ですが、それは私が何時間も立ち往生することに成功しました。ありがとう! – compuman

0

。あなたのSQLを見てください:

UPDATE Classmates SET fname=?,lname=?,age=?,gpa=? WHERE sid =? 

は、その後

   p = c.prepareStatement(updateSQL); 
        p.clearParameters(); 
        p.setInt(1, cid); 
        p.setString(2, firstname); 
        p.setString(3, lastname); 
        p.setDouble(4, age); 
        p.setDouble(5, gpa); 
        p.executeUpdate(); 

あなたがnamesidためにcid値を設定しているがgpaに設定されています。

また、実際に接続を閉じる必要があります。 try-with-resourcesを検討してください。

関連する問題