JDBC Java Database connection 3) statement interface

Source: Internet
Author: User

|-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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.