Statement:
By the name of the class, the class (metadata object) loaded into memory, load driver, because it is the data flow to do operations, must add exception handling, followed by
<span style= "White-space:pre" ></span>//Load a Class (metadata object) into memory by the name of the class <span style= "White-space:pre" > </span>class.forname ("Com.mysql.jdbc.Driver");
Create a database link, the first parameter in the Getconnection method refers to the database type and the name of the database being manipulated, the second parameter refers to the user name, and the third parameter refers to the password< password >
<span style= "White-space:pre" ></span>connection conn = drivermanager.getconnection (<span style= " White-space:pre "></span>" Jdbc:mysql://localhost:3306/score "," root "," 123456 ");
Then create a statement object
<span style= "White-space:pre" ></span>statement stm = Conn.createstatement ();
The general execution of SQL statements is used, if the query is returned is a two-dimensional relational table, so with the ResultSet object collection
<span style= "White-space:pre" ></span>//execution increment, delete, change the statement, return int type, the number of rows affected stm.executeupdate (strSQL);// Returns a ResultSet object, which is actually a table or view rst = Stm.executequery (strSQL);
ResultSet the object set, and then outputs the result, as follows
<span style= "White-space:pre" ></span>//output result while (Rst.next ()) {//cursor moves down one line System.out.println ( Rst.getstring ("Sid") + "\ T" + rst.getstring ("Sname");}
If the operation on the database is complete, you need to close the previously created object in Finally, and the object destruction follows the stack created
<span style= "White-space:pre" ></span><pre name= "code" class= "java" ><span style= "White-space: The pre "></span>finally {///Open is to open connection first, then turn resultset//off is the inverse of the off if (rst! = null) {try {rst.close ();} catch ( SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} if (STM = null) {try {stm.close ()} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} IF (conn! = null) {try {conn.close ()} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
PreparedStatement:
Package Jdbc_study;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.PreparedStatement; Import Java.sql.resultset;import Java.sql.sqlexception;public class Preparedstatementdemo {public static void main ( String[] args) {Connection conn = null; PreparedStatement stmt = null; ResultSet rst = null; String strSQL = "SELECT * FROM student where sid =?"; try {//through the name of the class, load the class (metadata object) into memory Class.forName ("Com.mysql.jdbc.Driver");//conn = Drivermanager.getconnection ("JDBC: Mysql://localhost:3306/score "," root "," 123456 ");//execute increment, delete, change statement, return int type, number of rows affected//pstm.executeupdate ();// Returns a ResultSet object, which is actually a table or view stmt = Conn.preparestatement (strSQL); stmt.setstring (1, "100081"); rst = Stmt.executequery ();//output result while (Rst.next ()) {//cursor moves down one line System.out.println (rst.getstring ("Sid") + "\ T" + rst.getstring ("Sname"));}} catch (ClassNotFoundException | SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();} finally {//Open first open connection, then open resultset// Off is the inverse of the off if (rst! = null) {try {rst.close ();} CAtch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} if (stmt! = null) {try {stmt.close ()} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} IF (conn! = null) {try {conn.close ()} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}}
Advantages and disadvantages of Statement and PreparedStatement:
preparedstatement
interface Inherits
Statement
interface
PreparedStatement is more
flexible and efficient
than ordinary
Statement objects .
improved readability and maintainability of the Code
improves the
SQL
performance of statement executionImproved Security