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 ();}