Jdbc connection to mysql database and CRUD "jdbc: mysql: // ip: Port/database name", user name, password
The Statement object in the lJdbc program is used to send SQL statements to the database. the common method of Statement object is executeQuery (String SQL): used to send query statements to data. ExecuteUpdate (String SQL): used to send an insert, update, or delete statement to a database execute (String SQL): used to send any SQL statement to the database addBatch (String SQL ): put multiple SQL statements in one batch. ExecuteBatch (): Sends a batch of SQL statements to the database for execution.
Package cn. outofmemory. test; import java. SQL. connection; import java. SQL. driverManager; public class Mysql {public static void main (String arg []) {try {Connection con = null; // defines a MYSQL link object Class. forName ("com. mysql. jdbc. driver "). newInstance (); // MYSQL driver con = DriverManager. getConnection ("jdbc: mysql: // 127.0.0.1: 3306/test", "root", "root"); // link to the local MYSQL System. out. print ("yes");} catch (Exception e) {System. out. print ("mysql error:" + e. getMessage ());}}}
// Insert a statement mport java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. statement; public class Mysql {/*** entry function * @ param arg */public static void main (String arg []) {try {Connection con = null; // define a MYSQL link object Class. forName ("com. mysql. jdbc. driver "). newInstance (); // MYSQL driver con = DriverManager. getConnection ("jdbc: mysql: // 127.0.0.1: 3306/test", "root", "root"); // link to the local MYSQL Statement stmt; // create the declaration stmt = con. createStatement (); // add a new data stmt.exe cuteUpdate ("insert into user (username, password) VALUES ('init', '123 ')"); resultSet res = stmt.exe cuteQuery ("select LAST_INSERT_ID ()"); int ret_id; if (res. next () {ret_id = res. getInt (1); System. out. print (ret_id) ;}} catch (Exception e) {System. out. print ("mysql error:" + e. getMessage ());}}}
// Delete and update data import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. statement; public class Mysql {/*** entry function * @ param arg */public static void main (String arg []) {try {Connection con = null; // define a MYSQL link object Class. forName ("com. mysql. jdbc. driver "). newInstance (); // MYSQL driver con = DriverManager. getConnection ("jdbc: mysql: // 127.0.0.1: 3306/test", "root", "root"); // link to the local MYSQL Statement stmt; // create the declaration stmt = con. createStatement (); // add a new data stmt.exe cuteUpdate ("insert into user (username, password) VALUES ('init', '123 ')"); resultSet res = stmt.exe cuteQuery ("select LAST_INSERT_ID ()"); int ret_id; if (res. next () {ret_id = res. getInt (1); System. out. print (ret_id);} // DELETE a data String SQL = "delete from user WHERE id = 1"; long deleteRes = stmt.exe cuteUpdate (SQL ); // if the value is 0, no delete operation is performed. if the value is greater than 0, the number of deleted entries is recorded. out. print ("DELETE:" + deleteRes); // UPDATE a data String updateSql = "UPDATE user SET username = 'xxxx' WHERE id = 2 "; long updateRes = stmt.exe cuteUpdate (updateSql); System. out. print ("UPDATE:" + updateRes);} catch (Exception e) {System. out. print ("mysql error:" + e. getMessage ());}}}
// Query data import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. resultSetMetaData; import java. SQL. statement; public class Mysql {/*** entry function * @ param arg */public static void main (String arg []) {try {Connection con = null; // define a MYSQL link object Class. forName ("com. mysql. jdbc. driver "). newInstance (); // MYSQL driver con = DriverManager. getConnection ("jdbc: mysql: // 127.0.0.1: 3306/test", "root", "root"); // link to the local MYSQL Statement stmt; // create the declaration stmt = con. createStatement (); // add a new data stmt.exe cuteUpdate ("insert into user (username, password) VALUES ('init', '123 ')"); resultSet res = stmt.exe cuteQuery ("select LAST_INSERT_ID ()"); int ret_id; if (res. next () {ret_id = res. getInt (1); System. out. print (ret_id);} // DELETE a data String SQL = "delete from user WHERE id = 1"; long deleteRes = stmt.exe cuteUpdate (SQL ); // if the value is 0, no delete operation is performed. if the value is greater than 0, the number of deleted entries is recorded. out. print ("DELETE:" + deleteRes); // UPDATE a data String updateSql = "UPDATE user SET username = 'xxxx' WHERE id = 2 "; long updateRes = stmt.exe cuteUpdate (updateSql); System. out. print ("UPDATE:" + updateRes); // query data and output String selectSql = "SELECT * FROM user"; ResultSet selectRes = stmt.exe cuteQuery (selectSql); while (selectRes. next () {// loop output result set String username = selectRes. getString ("username"); String password = selectRes. getString ("password"); System. out. print ("/r/n"); System. out. print ("username:" + username + "password:" + password) ;}} catch (Exception e) {System. out. print ("mysql error:" + e. getMessage ());}}}