JDBC Connection database code and procedures in full Java development

Source: Internet
Author: User
Tags stmt

  1. JDBC Connection Database
  2. ? Create a program that connects to the database in JDBC with 7 steps:
  3. 1. Load the JDBC driver:
  4. Before connecting to the database, first load the driver of the database you want to connect to the JVM (Java Virtual machine),
  5. This is achieved through the static method forname (String className) of the Java.lang.Class class.
  6. For example:
  7. Try {
  8. //Load the MySQL driver class
  9. Class.forName ("Com.mysql.jdbc.Driver");
  10. }catch(ClassNotFoundException e) {
  11. System.out.println ("Driver class not found, load driver failed!")   ");
  12. E.printstacktrace ();
  13. }
  14. After a successful load, an instance of the driver class is registered in the DriverManager class.
  15. 2. Provide the URL of the JDBC connection
  16. ? The connection URL defines the protocol, sub-protocol, and data source identity when the database is connected.
  17. Writing form: Protocol: Sub-Protocol: Data source identification
  18. Protocol: Always start with JDBC in JDBC
  19. Sub-Protocol: A bridge-connected driver or database management system name.
  20. Data source identification: The tag locates the address of the database source and the connection port.
  21. For example: (MySQL connection URL)
  22. Jdbc:mysql:
  23. //LOCALHOST:3306/TEST?USEUNICODE=TRUE&CHARACTERENCODING=GBK;    
  24. Useunicode=true: Indicates the use of the Unicode character set. If Characterencoding is set to
  25. gb2312 or GBK, this parameter must be set to true . CHARACTERENCODING=GBK: The character encoding method.
  26. 3. Create a connection to the database
  27. To connect to a database, you need to request and obtain a connection object from Java.sql.DriverManager.
  28. The object represents a connection to a database.
  29. ? Using the DriverManager getconnectin (string URL, string username,
  30. String password) method to pass in the path to the specified database to be connected, the user name of the database, and
  31. Password to get it.
  32. For example:
  33. //Connect MySQL database, username and password are root
  34. String url = "Jdbc:mysql://localhost:3306/test";
  35. String username = "root";
  36. String Password = "root";
  37. Try {
  38. Connection con =
  39. Drivermanager.getconnection (URL, username, password);
  40. }catch(SQLException se) {
  41. SYSTEM.OUT.PRINTLN ("Database connection failed!   ");
  42. Se.printstacktrace ();
  43. }
  44. 4. Create a statement
  45. To execute an SQL statement, you must obtain an java.sql.Statement instance that is divided into the following 3 statement instances
  46. Type of:
  47. 1. Execute static SQL statements.   Typically implemented through statement instances.
  48. 2. Execute dynamic SQL statements.   Typically implemented through PreparedStatement instances.
  49. 3. Execute the database stored procedure.   Typically implemented through CallableStatement instances.
  50. The specific implementation method:
  51. Statement stmt = Con.createstatement ();
  52. PreparedStatement pstmt = con.preparestatement (sql);
  53. CallableStatement cstmt =
  54. Con.preparecall ("{Call Demosp (?,?)}");
  55. 5. Execute SQL statements
  56. The statement interface provides three ways to execute SQL statements: ExecuteQuery, executeupdate
  57. and execute
  58. 1, ResultSet executeQuery (String sqlString): Execute SQL statement that queries the database
  59. A result set (ResultSet) object is returned.
  60. 2,int executeupdate (String sqlString): Used to perform INSERT, update, or
  61. Delete statements and SQL DDL statements, such as CREATE table and drop table
  62. 3. Execute (sqlString): Used to perform a return of multiple result sets, multiple update counts, or a combination of the two
  63. Statement.
  64. Specific implementation code:
  65. ResultSet rs = stmt.executequery ("SELECT * from ...");
  66. int rows = stmt.executeupdate ("INSERT into ...");
  67. boolean flag = Stmt.execute (String sql);
  68. 6. Processing Results
  69. Two cases:
  70. 1. The number of records affected by this operation is returned by performing the update.
  71. 2. The result of executing the query returned is a ResultSet object.
  72. ? ResultSet contains all rows that conform to the conditions in the SQL statement, and it provides a set of get methods for these
  73. Access to the data in the row.
  74. ? Get data using the access method of the result set (ResultSet) object:
  75. while (Rs.next ()) {
  76. String name = rs.getstring ("name");
  77. String pass = rs.getstring (1); //This method is more efficient
  78. }
  79. (columns are numbered from left to right and start with column 1)
  80. 7. Close the JDBC object
  81. All JDBC objects used are closed after the operation is complete to release the JDBC resource, turn off order harmony
  82. The opposite of the Ming Order:
  83. 1. Close record set
  84. 2. Closing the statement
  85. 3. Close the Connection object
  86. if (rs! = null) { //close recordset
  87. Try {
  88. Rs.close ();
  89. }catch(SQLException e) {
  90. E.printstacktrace ();
  91. }
  92. }
  93. if (stmt! = null) { //close declaration
  94. Try {
  95. Stmt.close ();
  96. }catch(SQLException e) {
  97. E.printstacktrace ();
  98. }
  99. }
  100. if (conn! = null) { //Close Connection object
  101. Try {
  102. Conn.close ();
  103. }catch(SQLException e) {
  104. E.printstacktrace ();
  105. }
  106. }

JDBC Connection database code and procedures in full Java development

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.