Reputation: 15
I am particularly new to the concept of accessing Databases in Java using JDBC. I am using MySql as my DBMS and I just wanted to know whether there is a way to insert records in my Database without using the "insert" statement of MySql?
Upvotes: 0
Views: 252
Reputation: 109002
Looking purely at JDBC, it is possible to add new rows into a database without using an INSERT
yourself. However there is no guarantee that the actual JDBC driver doesn't use an INSERT
statement.
try (
Connection con = DriverManager.getConnection(....);
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT col1, col2, col3 FROM someTable");
) {
rs.moveToInsertRow();
rs.updateString(1, "new value col1");
rs.updateString(2, "new value col2");
rs.updateString(3, "new value col3");
rs.insertRow();
}
This will insert a row through the result set, but the actual implementation might simply generate an INSERT
statement to achieve this. Also this way isn't very efficient (especially if the table has a a large number of rows), and it might fail if we haven't selected (or updated) columns that have constraints (for example a NOT NULL
or a CHECK
constraint).
There are also a number of frameworks built on top of JDBC that allow you to create and manipulate data without having to write queries yourself, however in the end chance are that it will generate an INSERT
queries.
So: from your perspective yes it is certainly possible to insert data into the database, but in the end the tool you use to do that will probably use INSERT
statements in some form.
Upvotes: 1