我正在使用Netbeans和SQLite一起使用我的DBMS。奇怪的是,我可以在我的数据库中添加记录,删除记录,并显示所有记录,但是,当我尝试更新记录时没有任何反应,甚至没有错误。有谁知道有什么问题?
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 :(得分:2)
您的更新代码有{{1}},但cid字段列在语句的最后:{{1}}
答案 1 :(得分:0)
您的WHERE
失败了。看看你的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();
您为cid
设置了name
值,sid
设置为gpa
。
另外,你真的应该关闭你的联系。考虑try-with-resources
。