Today reviewed the next JDBC operation database, in fact, through review, feel familiar with the class and the use of methods are trivial, after all, there are APIs can be found.
The key is some design,
1, for example:
Class.forName (""); this is used to load the driver, get the driver instance, and register it in the DriverManager during the loading process, which is managed by the latter.
So: in Drivermanager.getconnection (), the action that occurs is: DriverManager find the list that has been registered, and then according to the URL to correspond, and finally through driver to obtain the database connection, if there is a duplicate driver registration, The default driver that matches first is the correct result. Management mechanisms for some driver are provided in DriverManager, including some repetitive processing.
2, Preparedstatement,statement
The difference between the two: the former equivalent of SQL precompilation, when the program executes the SQL has been compiled well, although it can be dynamically transmitted, which also improves the efficiency of execution.
The former can also prevent SQL injection
The same preparedstatement can be executed multiple times if the underlying database and driver maintain the open state of these statements after the statement is committed. If this is not true, it is meaningless to try to improve performance by using PreparedStatement objects instead of Statement objects.
Example:
1 pstmt.setstring (1, "Hi"2 for (int i = 0; i <; i++3 Pstmt.setint (24int rowCount =5
3, ResultSet provides a good result set management mechanism, mainly realizes the Java.sql.ResultSet interface,
For example, see the result set has a few rows of columns, view the column names of the result set, see if the last row of the result set is empty, and so on.
4, for some long field input and output problems, see the code.
for(;;); Mt=con.createstatement (); ResultSet R= Stmt.executequery ("Select X from Table2"); //now get column 1 results with 4 K block size:byteBuff =New byte[4096]; while(R.next ()) {Java.io.InputStream fin= R.getasciistream (1); for (;;) { intSize =fin.read (buff);if(Size = =-1) {//reach end of stream Break; } //send the newly populated buffer to the ASCII output stream:Output.write (Buff, 0, size); } }
New java.io.File ("/tmp/data"int filelength =new=" UPDATE Table5 SET stuff =? WHERE index = 4 "); Pstmt.setbinarystream (1
The database provides three streaming mechanisms for long fields:
Getbinarystream
Getasciistream
Getunicodestream
where for (;;) is the meaning of infinite loops.
MySQL JDBC Summary