Steps to manipulate JDBC
1): Load the registration driver.
2): Gets the Connection object.
3): Create/Get Statement object
4): Execute SQL statement
5): Releasing resources
1 //Create a student table2 @Test3 Public voidtestcreatetable ()4 {5 6 7 //Database Connection Object8Connection conn =NULL;9 //To Create a statement objectTenStatement st =NULL; One //SQL statements AString sql = "CREATE TABLE t_student1 (id BIGINT PRIMARY KEY auto_increment,name VARCHAR (), age INT)"; - Try { - //JDK 1.6 mentions no need to load the registration driver the //class.forname ("Com.mysql.jdbc.Driver"); - //Get database Connection object -conn = Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test", "root", "123456"); - //Get Create statement object +St =conn.createstatement (); - //Execute SQL statement + introws =st.executeupdate (SQL); A}Catch(Exception e) { at //TODO auto-generated Catch block - e.printstacktrace (); - } - - //Freeing Resources - Try { in if(st!=NULL) - st.close (); to}Catch(Exception e) { + //TODO auto-generated Catch block - e.printstacktrace (); the}finally{ * Try { $ if(conn!=NULL)Panax Notoginseng conn.close (); -}Catch(Exception e) { the //TODO auto-generated Catch block + e.printstacktrace (); A } the } + -}
JDBC CREATE TABLE
JDBC connects to the database and creates a table