简体   繁体   English

使用mysql JDBC创建和插入值

[英]create and insert values using mysql JDBC

I have the sample code. 我有示例代码。

public void UpdateTable1() {
    for (int t = 0; t < 20; t++) {

        if (consumer == 1 && number == 1 && provider1 == 31 && feedback == 1) {
            try {
                Class.forName(driverName);
                con = DriverManager.getConnection(url + dbName, "root", "mysql");
                try {
                    Statement st = con.createStatement();
                    int val = st.executeUpdate("INSERT Consumer1 VALUES ("
                        + 31 + "," + "'Printer'" + ", " + 1 + " " + ")");
                    System.out.println("1 row affected");
                } catch (SQLException s) {
                    System.out.println("SQL statement is not executed!");
                }
                con.close();
            }
        }
    }
}

I want to insert the same set of values(31,printer,1) into the table consumer2,consumer3.Is it possible without using another try catch statements...Please help me. 我想将相同的一组值(31,printer,1)插入到表consumer2,consumer3中。是否可以在不使用其他try catch语句的情况下...请帮助我。

I hope I'm not offending, but are you sure you fully understand how try-catch works? 我希望我没有冒犯,但您确定您完全了解try-catch的工作原理吗?

The reason that the try catch statement is inside the for for t loop, is (likely) that someone wanted to ensure that one failure will not prevent other iterations of the loop from taking place. try catch语句位于for for t循环内的原因(可能是)有人希望确保一个故障不会阻止该循环的其他迭代。 You could end up with corrupted data that way. 这样可能会导致数据损坏。

The decision you have to make is whether you want to do all three insertions under the same try-catch or not. 您必须做出的决定是是否要在同一try-catch下进行所有三个插入。

To easily do three insertions, do a loop on i from 1 to 3, and every time create a different statement, by adding strings, so that the first time the table is Consumer1, the second time it is Consumer2, etc. Put the whole loop inside the try catch, or put the-try-catch inside the body of the loop. 要轻松进行三个插入,请在i上从1到3循环,然后每次通过添加字符串来创建不同的语句,这样表第一次是Consumer1,第二次是Consumer2,依此类推。在try catch中循环,或将try-catch放入循环主体中。

You could replace 您可以更换

"INSERT Consumer1 VALUES ("

with

"INSERT INTO Consumer" + (t + 1) + " VALUES ("

I'm a little unclear on this one. 我对此不清楚。 Is there some restriction on how many statements you can create per connection? 每个连接可以创建多少条语句是否有限制? Or will the connection close down after each update is executed? 还是在执行每次更新后关闭连接?

If not, then this should work 如果没有,那应该可以

Statement st2 = con.createStatement(); int val = st.executeUpdate("INSERT Consumer2 VALUES ("+ 31 + "," + "'Printer'" + ", " + 1 + " " + ")");
Statement st3 = con.createStatement(); int val = st.executeUpdate("INSERT Consumer3 VALUES ("+ 31 + "," + "'Printer'" + ", " + 1 + " " + ")");

if the connection closes down or you can only do one statement at a time, then I guess you could move the whole thing to a private method and then call it with different parameters, something like: 如果连接关闭或一次只能执行一条语句,那么我想您可以将整个操作移到私有方法,然后使用不同的参数调用它,例如:

private boolean doInsert(String tableName) {

con = DriverManager.getConnection(url + dbName, "root", "mysql");
try {
Statement st = con.createStatement();
int val = st.executeUpdate("INSERT " + tableName + " VALUES (" + 31 + "," + "'Printer'" + ", " + 1 + " " + ")"); System.out.println("1 row affected");
return true;
} catch (SQLException s) {
System.out.println("SQL statement is not executed!");
return false; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM