1, database Operation example:
Public static void jdbctest () throws exception {connection conn = null; string url = "Jdbc:mysql://localhost:3306/mysql_learn?" + "User=root&password=123qwe&useunicode=true&characterencoding=utf8"; string selectsql = "Select id,name,sex,is_del as isdel from mysql_ learn.t_user where name like '% King ' ";// one, load drive Try {class.forname (" Com.mysql.jdbc.Driver "); SYSTEM.OUT.PRINTLN ("Load driver succeeded!") ;// successfully loaded, an instance of the driver class is registered to the DriverManager class. Establish connection conn = drivermanager.getconnection (URL);// Three, create statement object// ? To execute an SQL statement, Java.sql.Statement instances must be obtained, and statement instances are divided into the following 3 types:// 1, execute static SQL statements. Typically implemented through statement instances. &NBSP;2, executes dynamic SQL statements. Typically implemented through PreparedStatement instances. &NBSP;3, executes the database stored procedure. Typically implemented through CallableStatement instances. Specific implementation:// statement stmt = con.createstatement () ;// PreparedStatement Pstmt = con.preparestatement (SqL) ;// callablestatement cstmt = con.preparecall ("{call demosp (? , ?)}") ; Statement stmt = conn.createstatement ();// IV, Executive sql// The statement interface provides three ways to execute SQL statements:executequery , executeupdate, and Execute// 1, Resultset executequery ( string sqlstring): Executes the SQL statement that queries the database// Returns a result set (ResultSet) object. 2, Int executeupdate (string sqlstring): Used to execute INSERT, UPDATE, or//&NBSP;DELETE statements, and SQL&NBSP;DDL statements, such as: Create table and Drop table,// 3, execute (sqlString): Used to perform a statement that returns multiple result sets, multiple update counts, or both. Implementation-specific code:// resultset rs = stmt.executequery ("SELECT&NBSP;*&NBSP;FROM&NBSP; ...") ;// int rows = stmt.executeupdate ("INSERT&NBSP;INTO&NBSP; ...") ;// boolean flag = stmt.execute (String sql) ; Resultset resultset = stmt.executequery (Selectsql);// Five, the result set processing// two kinds of situation:// 1, The update is performed to return the number of records affected by this operation. &NBSP;2, the result of executing a query returns a resThe Ultset object. ? resultset contains all rows that conform to the conditions in the SQL statement, and it provides access to the data in those rows through a set of Get methods. ? get data using the access method of the result set (ResultSet) object:// while (Rs.next ()) {// string name = Rs.getstring ("name") ;// string pass = rs.getstring (1) ; // This method is more efficient// }// (column is numbered from left to right and starts with column 1) list<user> users = new arraylist ();while ( Resultset.next ()) {user user = new user (); Resultset.getlong (1); ResultSet.getString (2) ; Resultset.getint (3); Resultset.getint (4); System.out.println ("Name:" + resultset.getstring (2));// Enter if the returned int type can be used Getint () users.add (user); Six, close the connection after the operation is complete, all the JDBC objects used are closed to release the JDBC resources, turn off the order and// the opposite:// 1, close the recordset// 2, close the Declaration/ 3, close Connection object if (Resultset != null) { // close recordset Resultset.close ();} if (stmt != null) { // Close Statement stmt.close ();} if (conn != null) { // Close Connection Object Conn.close ();}} catch (classnotfoundexception e) {system.out.println ("Driver class   not found; Load driver failed!") "); E.printstacktrace ();} catch (sqlexception e) {system.out.println ("MySQL operation Error! "); E.printstacktrace (); } finally { conn.close (); }}
2, MyBatis database operation example:
public static void Mybatistest () {try {//1, creating the input stream to read the configuration file Mybatis-config.xml string resource = "Mybatis-config.xml"; Reader reader = Resources.getresourceasreader (Resource);//2, creating sqlsessionfactorysqlsessionfactory factory = new Sqlsessionfactorybuilder (). build (reader);//3, create sqlsessionsqlsession session = Factory.opensession ();//4, Call the Mapper file to insert the data, you need to load the mapper file into the configuration file (mybatis-config.xml) in User user = new user (), User.setname ("Wang Jingkun"); User.setsex (1); User.setisdel (0); Session.insert ("Com.qding.mybatis.learn.model.User.insert", User); System.out.println (User.getid ()); System.out.println (User.getname ()); Session.commit (); Session.close ();} catch (IOException e) {e.printstacktrace ();}}
Mybatis, JDBC, habernate, mybatis+spring MySQL database operation example