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