JDBC 更新不显示错误但不工作?
Posted
技术标签:
【中文标题】JDBC 更新不显示错误但不工作?【英文标题】:JDBC Update Not Showing Errors But Not Working? 【发布时间】:2016-08-08 16:22:21 【问题描述】:我在我的 DBMS 中使用 Netbeans 和 SQLite。奇怪的是,我可以在我的数据库中添加一条记录、删除一条记录并显示所有记录,但是,当我尝试更新一条记录时,什么也没有发生,甚至没有错误。有谁知道是什么问题?
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');
【问题讨论】:
p.setInt( 1, cid );
根据你的更新查询,第一个参数是fname
您的字段与其在查询中的位置不匹配。
在 Stack Overflow 上,在收到对您有帮助的答案后丢弃您的问题被视为反社会行为。我已经回滚了你上次的编辑。
【参考方案1】:
您的更新代码有 p.setInt( 1, cid );
,但 cid 字段在语句中最后列出:String updateSQL = "UPDATE Classmates SET fname=?,lname=?,age=?,gpa=? WHERE sid =?";
【讨论】:
这是个问题,一个小问题,但它让我卡了几个小时。谢谢!【参考方案2】:您的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();
您正在为name
设置cid
值,而sid
设置为gpa
。
另外,你真的应该关闭你的连接。考虑try-with-resources
。
【讨论】:
以上是关于JDBC 更新不显示错误但不工作?的主要内容,如果未能解决你的问题,请参考以下文章
导入 CSV 文件时的 MySQL 问题 - 不显示错误,但不导入