|-Statement interface : used to execute a static SQL statement
|-int executeupdate (String sql) : performs a static update of the SQL statement (DDL,DML )
|-ResultSet executeQuery (String sql) : Static query SQL statement executed (DQL)
|-preparedstatement Interface (subclass of statement Interface): Used to perform precompilation SQL Statement
|-int executeupdate () : Perform precompiled update SQL statements (DDL,DML )
|-resultset executeQuery () : execute a precompiled query SQL statement (DQL)
Code: (using DDL statements for example, in fact, the DDL.DML.DQL of the steps in addition to the SQL statements sent are not the same, all the same)
1 Public classStatement_one {2 3 //information about the database4 //URL5 Private StaticString url = "jdbc:mysql://localhost:3306/day1029";6 //user name and password7 Private StaticString user = "root";8 Private StaticString password = "root";9 Ten //Execute DDL statements (CREATE table as an example) One Private Static voidDDL () { A //In order to close the connection, create the object here -Connection conn =NULL; -Statement STSM =NULL; the - Try { - //creating a Driver registration object -Class.forName ("Com.mysql.jdbc.Driver"); + - //Gets the connected Database object + /*Connection*/conn =drivermanager.getconnection (URL, user, A password); at - //Create a statement object - /*Statement*/STSM =conn.createstatement (); - - //create the SQL statement to execute -String sql = "CREATE TABLE person (id INT PRIMARY KEY auto_increment,name varchar (), sex VARCHAR (5), Age Int,psot varchar (1 0), email VARCHAR (+), phone INT) "; in - //when the DDL statement is sent using statement, the number of rows affected by the SQL statement is returned, and the Int class to intresult =stsm.executeupdate (SQL); +}Catch(Exception e) { - e.printstacktrace (); the } * //finally close the connection (turn off first) $ finally {Panax Notoginseng if(STSM! =NULL) { - Try { the stsm.close (); +}Catch(SQLException e) { A //TODO auto-generated Catch block the e.printstacktrace (); + Throw NewRuntimeException (e); - } $ } $ if(Conn! =NULL) { - Try { - conn.close (); the}Catch(SQLException e) { - //TODO auto-generated Catch blockWuyi e.printstacktrace (); the Throw NewRuntimeException (e); - } Wu } - } About}
JDBC Java Database connection 3) statement interface