Before learning the principles of the database, school also learned the Oracle database, My Learning video is about MySQL database, in fact, all the same, review the next SQL knowledge, database learning is not written down, from Java How to operate the database start it.
Because this year is over, start a new study, have time to learn while writing. In fact, there are many JDBC online tutorials, I read the video learning notes are as far as possible to write in the code. Just go directly to the code.
PackageCom.gh.jdbc;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;/*** PreparedStatement's setdate class is the java.sql packet of the Date * ResultSet getdate also back a java.sql.Date * sql. Date is a subclass of util.date that can go directly to util. Date * util. Date transfer sql.date can be new SQL. Date (New util. Date (). GetTime ()); * @authorGanhang **/ Public classJdbcdemo {//Driver Class Public StaticString driver= "Oracle.jdbc.OracleDriver"; //Database Address Public StaticString url= "Jdbc:oracle:thin: @localhost: 1521:orcl"; //Database user name Public StaticString user= "Scott"; //Database Password Public StaticString password= "."; /*** Find **/ Public Static voidfind () {Try{class.forname (Driver); Connection Conn=drivermanager.getconnection (URL, user, password); String SQL= "SELECT * FROM Test"; PreparedStatement PS=conn.preparestatement (SQL);//do not use Stateme, there is SQL injection problem, unsafeResultSet Rs=ps.executequery ();//return a result set (actually a reference) while(Rs.next ()) {System.out.println (Rs.getint (1)); } rs.close (); Ps.close (); Conn.close (); System.out.println ("Success!"); } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); } } //statement Implementation, unsafe Public Static voidInsert () {//Update Delete only changes SQL to Try { //Load DriverClass.forName (Driver); //Get the connection, note that the java.sql Connection,mysql package also has the connection interfaceConnection conn=drivermanager.getconnection (URL, user, password); //SQL statementsString sql= "INSERT into test values (100)"; //Get Statement Object//the description document has such a comment: By default, each Statement object can only open one ResultSet object at a time. //Therefore, if you read a ResultSet object and read another intersection, the two objects must be generated by different Statement objects. //If there is an open current ResultSet object for a statement, all execution methods in the Statement interface will be closed implicitly. Statement stat=conn.createstatement (); //Execute SQL (actually send SQL to database, database to perform similar scoket communication)stat.executeupdate (SQL); //Close related connection release resourcesStat.close (); Conn.close (); System.out.println ("Success!"); } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); } } //PreparedStatement Implementation Public Static voidInsert2 (intN) { Try{class.forname (Driver); Connection Conn=drivermanager.getconnection (URL, user, password); String SQL= "INSERT into test values (?)"; PreparedStatement PS=conn.preparestatement (SQL); Ps.setint (1, N); Ps.executeupdate (); Ps.close (); Conn.close (); System.out.println ("Success!"); } Catch(ClassNotFoundException e) {e.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); } } Public Static voidMain (string[] args) {//Insert (); //Insert2 (1);find (); }}
Java Learning JDBC