S.PRATHIBA
S.PRATHIBA

Reputation: 1

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.

Upvotes: 0

Views: 1299

Answers (3)

Lars Andren
Lars Andren

Reputation: 8781

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; }
}

Upvotes: 0

Uri
Uri

Reputation: 89749

I hope I'm not offending, but are you sure you fully understand how try-catch works?

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

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.

Upvotes: 1

trashgod
trashgod

Reputation: 205785

You could replace

"INSERT Consumer1 VALUES ("

with

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

Upvotes: 0

Related Questions