JDBC and jdbc connect to the database

Source: Internet
Author: User

JDBC and jdbc connect to the database
1. Configure the program-Let our program find the driver jar package of the database

1. Copy the. jar file to the project to facilitate integration.

2. In the eclipse project, right-click "build path"> "Configure build path"> "Database"> "add external jar"> find the driver jar package of the database. Then, click "OK. The "referenced library" will appear in the left-side package resource manager, in which you can find the jar package we just imported.

2. Write our program to call the driver package class and add operations to the data.

1. Load the data access driver
Class. forName ("com. mysql. jdbc. Driver ");

2. Connect to the database
Connection conn = DriverManager. getConnection ("jdbc: mysql: // 127.0.0.1: 3306/mydb", "root ","");

3. Construct and execute the SQL command .....
Statement state = conn. createStatement ();
State.exe cuteUpdate ("SQL statement for addition, deletion, and modification ");
// State.exe cuteQuery ("queried SQL statement ");

Conn. close ();

Public static void main (String [] args) throws Exception {// throws Exception must be added // 1. load the driver Class. forName ("com. mysql. jdbc. driver "); // com. mysql. jdbc package name. driver Class Name
// 2. connection conn = DriverManager. getConnection ("jdbc: mysql: // 127.0.0.1: 3306/mydb", "root", "");/* ("master protocol: Sub Protocol: // local ip Address: port Number/Database Name "," User Name "," password ") * // 3. construct the SQL Statement sta = conn. createStatement (); // enter the SQL statement String SQL = "insert into xs values ('51', 'AA', '20140901')"; sta.exe cuteUpdate (SQL ); // close the input stream and release the memory conn. close ();}

 

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.