Examples of this article are shown, mainly through the Java operation of SQL statements, and ordinary additions and deletions of the principle is the same:
ImportJava.sql.*; Public classTest { Public Static voidMain (string[] args)throwsException {class.forname ("Com.mysql.jdbc.Driver"); //A database that already exists must be filled in at the beginningString url = "Jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf-8"; Connection Conn= Drivermanager.getconnection (URL, "root", "123456"); Statement Stat=conn.createstatement (); //Create a database HelloStat.executeupdate ("CREATE Database Hello"); //Open the created databaseStat.close (); Conn.close (); URL= "Jdbc:mysql://localhost:3306/hello?useunicode=true&characterencoding=utf-8"; Conn= Drivermanager.getconnection (URL, "root", "123456"); Stat=conn.createstatement (); //CREATE TABLE TestStat.executeupdate ("CREATE TABLE test (ID int, name varchar (80))"); //Add DataStat.executeupdate ("INSERT into test values (1, ' Zhang San ')"); Stat.executeupdate ("INSERT into test values (2, ' John Doe ')"); //Querying DataResultSet result = Stat.executequery ("SELECT * FROM Test"); while(Result.next ()) {System.out.println (Result.getint ("id") + "" + result.getstring ("name"))); } //Close the databaseResult.close (); Stat.close (); Conn.close (); } }
Note: Reference link: http://blog.csdn.net/sd4015700/article/details/39668583
Java writing programs that create databases and tables