Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importjava.sql.Statement;Importorg.junit.Test; Public classDemo1 {PrivateString url = "Jdbc:mysql://localhost:3306/tablename"; PrivateString user = "root"; PrivateString password = "root"; @Test Public voidtest1 () {Connection conn=NULL; Statement stmt=NULL; Try{ //1. Registered DriverClass.forName ("Com.mysql.jdbc.Driver"); //2. Create a Connection objectconn =drivermanager.getconnection (URL, user, password); //3. Create a statement objectstmt =conn.createstatement (); //4. Preparing SQL statementsString sql = "CREATE trable student (id PRIMARY KEY auto_increment,name VARCHAR (), Gender (2))"; //5. Sending SQL statements, executing SQL statements intCount =stmt.executeupdate (SQL); //5. OutputSystem.out.println ("affected" + Count + "line! "); }Catch(Exception e) {e.printstacktrace (); Throw NewRuntimeException (e); }finally{ //7, close the connection (order: After opening the first closed) if(stmt! =NULL){ Try{stmt.close (); } Catch(SQLException e) {e.printstacktrace (); Throw NewRuntimeException (e); } } if(Conn! =NULL){ Try{conn.close (); } Catch(SQLException e) {e.printstacktrace (); Throw NewRuntimeException (e); } } } }}
statement executing static SQL statements