Definition of JDBC:
Java Data Base Connectivity,java database connection; is a Java API for executing SQL statements;
The steps of the JDBC operation:
1. Registration Driver
Class.forName ();
2. Get the connection
Connection xxx=drivermanager.getconnection ();
3. Writing SQL
String sql= "";
4. Create PRECOMPILED statement performers
Preparestatement yyy=xxx.preparestatement (Sql,username,password);
5. Setting parameters
Yyy.getstring () or yyy.getint ();
6. Execute the query
Query operations
Yyy.executequery ();
Adding and removing changes operation
Yyy.executeupdate ();
7. Releasing Resources
???. close ();
Case:
1 PackageCn.it.jdbc;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.PreparedStatement;6 ImportJava.sql.ResultSet;7 Importjava.sql.SQLException;8 Importjava.sql.Statement;9 Ten Importorg.junit.Test; One A Importcom.jdbc.utlis.jdbcUtils; - ImportCom.jdbc.utlis.jdbcUtilsDemo; - the Public classHellodemo { - @Test - Public voidMYJDBC ()throwsClassNotFoundException, SQLException { - //1. Registration Driver +Class.forName ("Com.mysql.jdbc.Driver"); - //2. Get the connection +Connection Connection =Drivermanager.getconnection ( A"Jdbc:mysql://localhost:3306/day01", "root", "root"); at //3. Writing SQL -String sql = "SELECT * from User"; - //4. Create a statement performer -PreparedStatement statement =connection.preparestatement (SQL); - //5. Setting Parameters - in //6. Execute SQL statements -ResultSet ResultSet =statement.executequery (); to //7. Traverse the results + while(Resultset.next ()) { -System.out.println (resultset.getstring ("id") + "----" the+ resultset.getstring ("username")); * $ }Panax Notoginseng resultset.close (); - statement.close (); the connection.close (); + } A the //Test jdbcutils test method + @Test - Public voidUtils () { $Connection Connection =NULL; $PreparedStatement statement =NULL; - - //Get Connections the Try { -Connection =jdbcutils.getconnection ();Wuyi //Writing SQL statements theString sql = "INSERT into user VALUES (?,?)"; - //Statement pre-compilation Wustatement =connection.preparestatement (SQL); - //Setting Parameters AboutStatement.setint (1, 5); $Statement.setstring (2, "Xiaoming"); - //Execute SQL - inti =statement.executeupdate (); - //To determine if the statement executed successfully A if(i = = 1) { +SYSTEM.OUT.PRINTLN ("OK!!!"); the}Else { -SYSTEM.OUT.PRINTLN ("NO!!!"); $ } the the}Catch(ClassNotFoundException e) { the //TODO auto-generated Catch block the e.printstacktrace (); -}Catch(SQLException e) { in //TODO auto-generated Catch block the e.printstacktrace (); the}finally { About jdbcutils.closestatent (statement); the Jdbcutils.closeconn (connection); the the } + - } the Bayi //Update Action the @Test the Public voidUpdate () { -Connection Connection =NULL; -PreparedStatement statement =NULL; the Try { the //Registration Driver theClass.forName ("Com.mysql.jdbc.Driver"); the //Get Connections -Connection =Drivermanager.getconnection ( the"Jdbc:mysql://localhost:3306/day01", "root", "root"); the //SQ1 Statements theString sql = "UPDATE user SET username=?" where id=? ";94 //Create statement performer thestatement =connection.preparestatement (SQL); the //Setting Parameters theStatement.setstring (1, "Er kang");98Statement.setint (2, 6); About //Execute SQL statement - statement.executeupdate ();101}Catch(ClassNotFoundException e) {102 //TODO auto-generated Catch block103 e.printstacktrace ();104}Catch(SQLException e) { the //TODO auto-generated Catch block106 e.printstacktrace ();107 }108 Try {109 statement.close (); the connection.close ();111}Catch(SQLException e) { the //TODO auto-generated Catch block113 e.printstacktrace (); the } the the }117 118 //Delete Operation119 @Test - Public voidDelete () {121Connection Connection =NULL;122PreparedStatement preparestatement =NULL;123 Try {124Class.forName ("Com.mysql.jdbc.Driver"); theConnection =Drivermanager.getconnection (126"Jdbc:mysql://localhost:3306/day01", "root", "root");127String sql = "Delete from user where id=?"; -Preparestatement =connection.preparestatement (SQL);129Preparestatement.setint (1, 5); the inti =preparestatement.executeupdate ();131 if(i = = 1) { theSystem.out.println ("OK");133 }134}Catch(ClassNotFoundException e) {135 //TODO auto-generated Catch block136 e.printstacktrace ();137}Catch(SQLException e) {138 //TODO auto-generated Catch block139 e.printstacktrace (); $ }141 142 Try {143 preparestatement.close ();144 connection.close ();145}Catch(SQLException e) {146 //TODO auto-generated Catch block147 e.printstacktrace ();148 }149 Max }151}
JDBC
Understanding of JDBC Fundamentals in Java EE learning