gpt4 book ai didi

java - JDBC 更新未显示错误但不起作用?

转载 作者:行者123 更新时间:2023-12-02 03:26:21 25 4
gpt4 key购买 nike

我使用 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');
}
}

最佳答案

您的更新代码有 p.setInt( 1, cid ); 但 cid 字段列在语句的最后: String updateSQL = "UPDATE Classmates SET fname=?,lname= ?,age=?,gpa=?WHERE sid =?";

关于java - JDBC 更新未显示错误但不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38834151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com