Java simple MySQL database connection addition, deletion, modification, and query _ MySQL

Source: Internet
Author: User
Java simple MySQL database connection addition, deletion, modification, query 1. connect to the database and obtain the database connection variable

[Java]View plaincopyprint?

  1. // Note that this is the method for connecting to mysql

// Note that this is the method for connecting to mysql

Note that when connecting to the database

(1) open the DB Browser to create a new Database Driver, pay attention to add the Driver JARs when the package, my is the mysql-connector-java-5.0.3-bin.jar

(2) copy the jar package of the database to the WEB-INF \ lib under the project

[Java]View plaincopyprint?

  1. Import java. SQL. Connection; // java package
  2. Public class DBConnection
  3. {
  4. Private String dbDriver = "com. mysql. jdbc. Driver ";
  5. Private String dbUrl = "jdbc: mysql: // [IP address]: [port number]/[Database name]"; // varies depending on the actual situation
  6. Private String dbUser = "root ";
  7. Private String dbPass = "root ";
  8. Public Connection getConn ()
  9. {
  10. Connection conn = null;
  11. Try
  12. {
  13. Class. forName (dbDriver );
  14. }
  15. Catch (ClassNotFoundException e)
  16. {
  17. E. printStackTrace ();
  18. }
  19. Try
  20. {
  21. Conn = DriverManager. getConnection (dbUrl, dbUser, dbPass); // note that three parameters
  22. }
  23. Catch (SQLException e)
  24. {
  25. E. printStackTrace ();
  26. }
  27. Return conn;
  28. }
  29. }

Import java. SQL. connection; // java package public class DBConnection {private String dbDriver = "com. mysql. jdbc. driver "; private String dbUrl =" jdbc: mysql: // [IP address]: [port number]/[Database name] "; // change according to the actual situation private String dbUser = "root"; private String dbPass = "root"; public Connection getConn () {Connection conn = null; try {Class. forName (dbDriver);} catch (ClassNotFoundException e) {e. printStackTrace ();} try {conn = DriverManager. getConnection (dbUrl, dbUser, dbPass); // note that there are three parameters} catch (SQLException e) {e. printStackTrace () ;}return conn ;}}

2. INSERT operation

[Java]View plaincopyprint?

  1. Public int insert ()
  2. {
  3. Int I = 0;
  4. String SQL = "insert into (table name) (column name 1, listing 2) values (?,?) ";
  5. Connection cnn = getConn ();
  6. Try {
  7. PreparedStatement preStmt = cnn. prepareStement (SQL );
  8. PreStmt. setString (1, value );
  9. PreStmt. setString (2, value); // Or: preStmt. setInt (1, value );
  10. I =preStmt.exe cuteUpdate ();
  11. }
  12. Catch (SQLException e)
  13. {
  14. E. printStackTrace ();
  15. }
  16. Return I; // return the number of affected rows. 1 indicates that execution is successful.
  17. }

Public int insert () {int I = 0; String SQL = "insert into (table name) (column name 1, listing 2) values (?,?) "; Connection cnn = getConn (); try {PreparedStatement preStmt = cnn. prepareStement (SQL); preStmt. setString (1, value); preStmt. setString (2, value); // Or: prestmt.setint(1. The value of the catch parameter i1_prestmt.exe cuteUpdate ();} catch (SQLException e) {e. printStackTrace ();} return I; // return the number of affected rows. 1 indicates that execution is successful}
3. update operations

[Java]View plaincopyprint?

  1. Public int update
  2. {
  3. Int I = 0;
  4. String SQL = "update (table name) set (column name 1) = ?, List 2 =? Where (column name) =? "; // Note that the where condition must exist.
  5. Connection cnn = getConn ();
  6. Try {
  7. PreparedStatement preStmt = cnn. prepareStatement (SQL );
  8. PreStmt. setString (1, (value ));
  9. PreStmt. setString (2, (value); // Or: preStmt. setInt (1, value );
  10. PreStmt. setInt (3, (value ));
  11. I =preStmt.exe cuteUpdate ();
  12. }
  13. Catch (SQLException e)
  14. {
  15. E. printStackTrace ();
  16. }
  17. Return I; // return the number of affected rows. 1 indicates that execution is successful.
  18. }

Public int update {int I = 0; String SQL = "update (table name) set (column name 1) = ?, List 2 =? Where (column name) =? "; // Note that the where condition Connection cnn = getConn () is required; try {PreparedStatement preStmt = cnn. prepareStatement (SQL); preStmt. setString (1, (value); preStmt. setString (2, (value); // Or: prestmt.setint(1. The value of prestmt.setint(3,(value of commandid i1_prestmt.exe cuteUpdate ();} catch (SQLException e) {e. printStackTrace ();} return I; // return the number of affected rows. 1 indicates that execution is successful}



4. search operation

[Java]View plaincopyprint?

  1. Public String select
  2. {
  3. String SQL = "select * from (table name) where (column name) = (value )";
  4. Connection cnn = getConn (); // The Connection is obtained through the self-written method getConn ().
  5. Try
  6. {
  7. Statement stmt = conn. createStatement ();
  8. ResultSet rs = stmt.exe cuteQuery (SQL );
  9. If (rs. next ())
  10. {
  11. Int m1 = rs. getInt (1); // or rs. getString (1). it is determined based on the value type of the column in the database. the parameter is the first column.
  12. String m2 = rs. getString (2 );
  13. }
  14. // Write the searched value to the class and return the corresponding object
  15. }
  16. Catch (SQLException e)
  17. {
  18. E. printStackTrace ();
  19. }
  20. Return (variable of the corresponding value );
  21. }

Public String select {String SQL = "select * from (table name) where (column name) = (value)"; Connection cnn = getConn (); // The getConn () method is used to obtain the connection try {Statement stmt = conn. createStatement (); ResultSet rs = stmt.exe cuteQuery (SQL); if (rs. next () {int m1 = rs. getInt (1); // or rs. getString (1) is determined based on the value type of the column in the database. the parameter is the first String m2 = rs. getString (2) ;}// you can write the searched value to the class and return the corresponding object} catch (SQLException e) {e. printStackTrace () ;}return (variable of the corresponding value );}

5. delete operation

[Java]View plaincopyprint?

  1. Public int delete ()
  2. {
  3. String SQL = "delete from (table name) where (column name) = (value )";
  4. Int I = 0;
  5. Connection conn = getConn (); // The Connection is obtained through the self-written method getConn ().
  6. Try
  7. {
  8. Statement stmt = conn. createStatement ();
  9. I = stmt.exe cuteUpdate (SQL );
  10. }
  11. Catch (SQLException e)
  12. {
  13. E. printStackTrace ();
  14. }
  15. Return I; // if return is 1, the execution is successful;
  16. }

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.