简体   繁体   English

使用自定义方法插入我的表不起作用,但如果我输入创建连接方法它工作正常

[英]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:我认为你在这里有两个问题:

  1. 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()

  2. 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.

相关问题 我有一个密码。 正常工作,直到我在其中添加了其他方法 - I have a code. It works fine till I put an extra method in it Java:如何创建适用于日期类的方法 - Java: How can I create method that works on my date class 我的自定义缩放方法不起作用 - My custom scaling method not working 如何创建动画方法并将其放入绘画方法? 我的代码不起作用 - How can I create an animation method and put that in paint method? My code doesn't work 为什么我的数组从方法返回空白,但在 main-Method 中运行时代码工作正常? - Why are my arrays returned blank from a method but the code works fine when running in the main-Method? 如何在活动中创建方法以将其放入自定义适配器列表视图中? - how to create method in activity to put it in custom adapter listview? 我使用 java 创建了一个 api 方法我想在我的 talend 集成中使用它,但我不知道如何将该方法放入 talend 中使用 - I created an api method using java i wanted to use it in my talend intergration but i dont know how to put the method in talend to use 在NULL上进行方法调用可以正常工作-期待NPE - Method invocation on NULL works fine - Expecting NPE 单击按钮时使用静态方法将值插入数据库表 - Inserting values into database table using static method when button is clicked 如何使用Restful Web服务使用put方法将数据插入到MySQL数据库中 - How can I use put method to insert data to my MySQL databse using restful webservice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM