Java writing programs that create databases and tables

Source: Internet
Author: User

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

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.