import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "root", "");
conn.setAutoCommit(false);
Statement st = conn.createStatement();
st.addBatch("INSERT INTO products (id, name, quantity, price) VALUE ('1', 'M', 10.99, 20)");
st.addBatch("INSERT INTO products (id, name, quantity, price) VALUE ('2', 'D', 15.99, 19)");
st.addBatch("INSERT INTO products (id, name, quantity, price) VALUE ('3', 'T', 5.99, 25)");
st.addBatch("INSERT INTO products (id, name, quantity, price) VALUE ('4', 'S', 8.99, 50)");
int[] updateCounts = st.executeBatch();
conn.commit();
} catch (SQLException e) {
if (conn != null) {
conn.rollback();
}
e.printStackTrace();
} finally {
if (conn!= null) {
conn.close();
}
}
}
}
|