Jdbc connection to mysql database and CRUD_MySQL

Source: Internet
Author: User
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 ());}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.