已关闭,此问题需要更focused。目前不接受答复。
**想改善这个问题吗?**更新问题,使其仅通过editing this post关注一个问题。
8天前关闭
Improve this question
插入后的标志值:假javasql.SQLSyntaxErrorException:510 labs桌。您访问的页面不存在。mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
我试图在JavaFX的帮助下在Java代码中执行一些操作,但遇到了这个DB错误。**编码:**包型号;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import controller.FOS_Constants;
//import controller.CustomerLogin;
public class CustomerDetailDB {
private DBConnect dbc;
@SuppressWarnings("unused")
private Connection conn;
private Statement stmt = null;
private ResultSet result = null;
public CustomerDetailDB() throws SQLException {
dbc = new DBConnect();
}
// Rest of the class code
// Create table for storing User Details
public void createUserDetailDBTable() {
try {
// Open a connection
System.out.println("Connecting to database to create UserDetailDB Table...");
System.out.println("Connected database successfully...");
// Execute create query
System.out.println("Creating UserDetailDB table in given database...");
stmt = dbc.connect().createStatement();
String sql = "CREATE TABLE 510labs.ofos_userdetaildb_02 " +
"(user_id INTEGER NOT NULL AUTO_INCREMENT, " + " username VARCHAR(20), " +
" email VARCHAR(30), " + " address VARCHAR(50), " + " phone VARCHAR(10), " +
" password VARCHAR(15), " + " PRIMARY KEY(user_id))";
String sql1 = "CREATE TABLE " + FOS_Constants.getCustomerTableName()
+ "(user_id INTEGER NOT NULL AUTO_INCREMENT, " + " username VARCHAR(20), " + " email VARCHAR(30), "
+ " address VARCHAR(50), " + " phone VARCHAR(10), " + " password VARCHAR(15), "
+ " PRIMARY KEY(user_id))";
stmt.executeUpdate(sql1);
System.out.println("Created UserDetailDB table in given database...");
dbc.connect().close(); // close db connection
} catch (SQLException se) { // Handle errors for JDBC
se.printStackTrace();
}
}
public boolean insertUserDetailRecords(String username, String email, String address, String phone,
String password) {
Boolean flag = false;
try {
stmt = dbc.connect().createStatement();
String sql = null;
// CustomerLogin clpcObj = new CustomerLogin();
sql = "INSERT INTO " + FOS_Constants.getCustomerTableName() + "(username,email,address,phone,password) "
+ "VALUES ('" + username + "', '" + email + "', '" + address + "', '" + phone + "', '"
+ password + "' )";
stmt.executeUpdate(sql);
System.out.println("Records Inserted into UserDetailDB.");
flag = true;
dbc.connect().close();
} catch (SQLException se) {
se.printStackTrace();
}
System.out.println("flag value after insert: " + flag);
return flag;
}
public boolean searchUserDetailDB(String username, String password) {
boolean flag = false;
try {
stmt = dbc.connect().createStatement();
String sql = null;
sql = "SELECT username, password FROM " + FOS_Constants.getCustomerTableName() + " WHERE username = '"
+ username + "' AND password = '" + password + "' ";
ResultSet rs = stmt.executeQuery(sql);
System.out.println("sql: "+sql);
System.out.println("rs: "+rs);
if (rs.next())
flag = true;
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
public int getIdFromUserDetailDB(String username, String password) {
@SuppressWarnings("unused")
boolean flag = false;
int userId = 0;
try {
stmt = dbc.connect().createStatement();
String sql = null;
sql = "SELECT user_id FROM " + FOS_Constants.getCustomerTableName() + " WHERE username = '"
+ username + "' AND password = '" + password + "' ";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
flag = true;
userId = rs.getInt(1);
System.out.println("userId: "+userId);
}
} catch (SQLException e) {
e.printStackTrace();
}
return userId;
}
public ResultSet fetchCustDetailsFromDB() throws SQLException {
dbc = new DBConnect();
stmt = dbc.connect().createStatement();
String query = "SELECT * FROM " + FOS_Constants.getCustomerTableName();
ResultSet rs = stmt.executeQuery(query);
dbc.connect().close();
return rs;
}
public void updateCustomerDetails(int custid, String custname, String custemail, String custaddr, String custphone)
throws SQLException {
@SuppressWarnings("unused")
Connection conn = null;
dbc = new DBConnect();
System.out.println("Entered update method");
System.out.println("custid: "+custid+"custname: "+custname+"custemail: "+custemail+"custaddr: "+custaddr+"custphone: "+custphone);
stmt = dbc.connect().createStatement();
String query = "Update " + FOS_Constants.getCustomerTableName() + " SET username = '" + custname
+ "' , email = '" + custemail + "' , address = '" + custaddr + "' , phone = '" + custphone
+ "' WHERE user_id = '" + custid + "'";
System.out.println("update query: "+query);
stmt.executeUpdate(query);
dbc.connect().close();
}
public void deleteCustomer(int id) {
@SuppressWarnings("unused")
Connection conn = null;
dbc = new DBConnect();
try {
stmt = dbc.connect().createStatement();
String sql = "DELETE FROM " + FOS_Constants.getCustomerTableName() + " where user_id = '" + id + "'";
stmt.executeUpdate(sql);
dbc.connect().close();
} catch (SQLException e) {
System.out.println(e);
}
}
public ResultSet retrieveValues(int id) throws SQLException {
System.out.println("Retrieve Values in Customer Detail DB!!!"+ id);
@SuppressWarnings("unused")
Connection conn = null;
dbc = new DBConnect();
stmt = dbc.connect().createStatement();
String sql = "Select * from "+ FOS_Constants.getCustomerTableName() +" where user_id = '" + id + "'";
result = stmt.executeQuery(sql);
dbc.connect().close();
return result;
}
}
我不擅长java代码,所以没怎么尝试
1条答案
按热度按时间gpfsuwkq1#
错误消息显示您的代码中存在SQL语法错误,特别是这一行:
导致
SQLSyntaxErrorException
解决方案:检查您的
Database Schema
,以确保该表存在并正确写入。如果表不存在,请创建一个。