[英]Inserting into my table using a custom method is not working, but if I put in the create connection method it works fine
Inserting into my table using a custom method is not working, but if I put in the create connection method it works fine.使用自定义方法插入我的表是行不通的,但如果我输入创建连接方法它工作正常。
This is the method I wrote to insert into the table, this method is in a different file, the caller method is also in a different file.这是我写的插入表格的方法,这个方法在不同的文件中,调用者方法也在不同的文件中。 When the method is called, it does nothing but stops the program execution with no errors.
调用该方法时,它什么也不做,只是停止程序执行而没有错误。
void insertIntoPatientTable(@NotNull Connection conn){
System.out.println("inside the insert");
final String insertStatement =
"INSERT INTO Patients(FIRST_NAME, LAST_NAME, AGE, " +
"PHONE_NUMBER, NEXT_OF_KIN_NAME, NEXT_OF_KIN_PHONE_NUMBER)" +
"VALUES( ? , ? , ? , ? , ? , ?)" ;
try{
System.out.println("inside try block");
System.out.println(conn.isClosed());
PreparedStatement preparedStatement = conn.prepareStatement(insertStatement);
preparedStatement.setString(1, "John");
preparedStatement.setString(2, "Doe");
preparedStatement.setInt(4, 24);
preparedStatement.setString(3, "will");
preparedStatement.setInt(5, 34);
preparedStatement.setInt(6, 14);
preparedStatement.execute();
System.out.println("executed");
}catch (SQLException ex){
ex.printStackTrace();
}
}
This is the method that calls it.这是调用它的方法。 Each time I call the method, it doesn't seem to do anything:
每次我调用该方法时,它似乎都没有做任何事情:
void createConnection() {
setPASSWORD(" ");
try{
setConn(DriverManager.getConnection(getUrlString().concat(getTIMEZONE()), getUSER(), getPASSWORD()));
Connection dbConnection = getConn();
CRUD_Statements patientModel = new CRUD_Statements();
Statement statement = dbConnection.createStatement();
String userStatement = patientModel.createPatientTable();
patientModel.insertIntoPatientTable(dbConnection);
statement.execute(userStatement);
List<PatientModel> patient = new ArrayList<>();
}catch(SQLException ex){
ex.printStackTrace();
}
}
I think that you have two problems here:我认为你在这里有两个问题:
You invoke patientModel.insertIntoPatientTable
before creating the table with statement.execute(userStatement)
.在使用
statement.execute(userStatement)
创建表之前调用patientModel.insertIntoPatientTable
。 If the table really doesn't exist before you try to insert into it, insertIntoPatientTable
is going to throw an Exception, but you're catching the Exception and not displaying it.如果在您尝试插入之前该表确实不存在,则
insertIntoPatientTable
将引发异常,但您正在捕获异常并且不显示它。 Try replaceing ex.getMessage()
with ex.printStacktrace()
尝试用 ex.printStacktrace
ex.getMessage()
替换ex.printStacktrace()
If insertIntoPatientTable
, it's going to close the connection, but then you try to re-use the connection in createConnection
after insertIntoPatientTable
returns.如果
insertIntoPatientTable
,它将关闭连接,但是在insertIntoPatientTable
返回后,您尝试在createConnection
中重新使用连接。
So, make sure that you log all of your exceptions, and don't close your connection until you're really done with it.因此,请确保记录所有异常,并且在真正完成之前不要关闭连接。
You'll probably also have a problem with creating the table if you run this a second time, as the table will already exist.如果您再次运行此程序,您可能还会在创建表时遇到问题,因为该表已经存在。
Finally, if the table doesn't exist, you'll need to create before you try to access it, not after.最后,如果表不存在,您需要在尝试访问它之前创建,而不是之后。
it has been fixed.它已被修复。 the problem was with the get message, immediately i printed the stackTrace i found out it was throwing errors the getMessage() wasn't picking.
问题出在 get 消息上,我立即打印了 stackTrace,我发现它抛出了 getMessage() 没有选择的错误。 Apparently it was a problem with my datatype in the table.
显然这是我在表中的数据类型的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.