MySQL uses statement to realize DDL,DML,DQL's operation demo

Source: Internet
Author: User

Demo1

Connection connection=NULL; Statement stmt=NULL; intResult=-1; Try{class.forname ("Com.mysql.jdbc.Driver"); } Catch(ClassNotFoundException e) {e.printstacktrace (); }                        Try {            //Create a connectionString url= "JDBC:MYSQL://LOCALHOST:3306/JDBCDB"; Connection= Drivermanager.getconnection (URL, "root", "MySQL"); } Catch(SQLException e) {e.printstacktrace (); }                Try {            //Create statementString sql= "CREATE TABLE s_user (id INT PRIMARY KEY auto_increment, NAME VARCHAR (), PASSWORD VARCHAR (15))"; stmt=connection.createstatement (); //executes the SQL statement, returning the number of rows affected ———— int valueresult=stmt.executeupdate (SQL); } Catch(SQLException e) {e.printstacktrace (); } System.out.println ("Result=" +result); Try {            //Close the stream            if(stmt!=NULL) {stmt.close (); }                        if(connection!=NULL) {connection.close (); }                    } Catch(SQLException e) {e.printstacktrace (); }

Demo2

Connection connection=NULL; Statement stmt=NULL; ResultSet RSet=NULL; Try{class.forname ("Com.mysql.jdbc.Driver"); String URL= "JDBC:MYSQL://LOCALHOST:3306/JDBCDB"; String User= "Root"; String Password= "MySQL"; //Connectionconnection=drivermanager.getconnection (URL, user, password); stmt= Connection.createstatement ();//StatementString sql= "select * from S_user;"; RSet= Stmt.executequery (SQL);//Execute SQL Statement---dataset (similar to map)                         while(Rset.next ()) {//get Value (by index)                intId= Rset.getint (1); String name=rset.getstring (2); String pwd=rset.getstring (3); System.out.println ("Id=" +id+ "name=" +name+ ";p wd=" +pwd); //Pass the row numberId=rset.getint ("id"); Name=rset.getstring ("name"); PWD=rset.getstring ("Password"); System.out.println ("~~~~~~~id=" +id+ "name=" +name+ ";p wd=" +pwd); }                    } Catch

MySQL uses statement to realize DDL,DML,DQL's operation demo

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.