JDBC connects to the database and creates a table

Source: Internet
Author: User

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

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.