Transfer from Weibo: http://www.cnblogs.com/centor/p/6142775.html
Tools: Eclipse
MySQL5.6
MySQL Connection driver: Mysql-connector-java-5.1.27.jar (link: https://pan.baidu.com/s/1MmFZ9Hve6rV0tlryM3raeA password: q74m)
Load driver:
1. Create the Lib folder in the project catalog and place the downloaded JDBC in the folder as shown in:
2. Right-click the project name, select Add JARs in the Libraries page in Java build path ..., select the JDBC you just added, such as:
Packet Preparation:
In database Sqltestdb, create the following data table EMP:
CREATE TABLE emp ( empno INT (4) PRIMARY KEY, ename varchar (ten), job VARCHAR (9), HireDate DATE, sal FLOAT (7,2));
Add Data:
Connect to the database and read the data:
Database name: Sqltestdb
Packet Name: EMP
Port number: 3306
User name: Root
Password: root
1 PackageSqldemo;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 ImportJava.sql.ResultSet;6 Importjava.sql.SQLException;7 Importjava.sql.Statement;8 9 Public classMain {Ten One Public Static voidMain (string[] args) { A //declaring connection Objects - Connection con; - //driver name theString Driver = "Com.mysql.jdbc.Driver"; - //the URL points to the database name you want to access MyData -String url = "Jdbc:mysql://localhost:3306/sqltestdb"; - //user name when MySQL is configured +String user = "root"; - //password for MySQL configuration +String password = "123456"; A //traversing query result sets at Try { - //Load Driver - Class.forName (driver); - //1.getConnection () method, connect MySQL database!! -Con =drivermanager.getconnection (Url,user,password); - if(!con.isclosed ()) inSYSTEM.OUT.PRINTLN ("Succeeded connecting to the database!"); - //2. Create the statement class object to execute the SQL statement!! toStatement Statement =con.createstatement (); + //the SQL statement to execute -String sql = "SELECT * from emp"; the //3.ResultSet class, used to store the obtained result set!! *ResultSet rs =statement.executequery (SQL); $System.out.println ("-----------------");Panax NotoginsengSystem.out.println ("The execution results are as follows:"); -System.out.println ("-----------------"); theSystem.out.println ("name" + "\ T" + "title"); +System.out.println ("-----------------"); A theString job =NULL; +String ID =NULL; - while(Rs.next ()) { $ //get stuname This column of data $Job = rs.getstring ("Job"); - //get stuid This column of data -id = rs.getstring ("ename"); the - //Output ResultsWuyiSystem.out.println (id + "\ T" +job); the } - rs.close (); Wu con.close (); -}Catch(ClassNotFoundException e) { About //database-driven class exception handling $System.out.println ("Sorry,can ' t find the driver!"); - E.printstacktrace (); -}Catch(SQLException e) { - //database connection failure exception handling A E.printstacktrace (); +}Catch(Exception e) { the //Todo:handle Exception - e.printstacktrace (); $}finally{ theSYSTEM.OUT.PRINTLN ("Database data successfully obtained!! "); the } the } the -}
Operation Result:
Add, delete, and modify data:
Add Data:
String name; String ID; PreparedStatement Psql; ResultSet Res;//preprocessing adds data, which has two parameters--"? "Psql = Con.preparestatement ("INSERT into EMP (empno,ename,job,hiredate,sal)" + "VALUES (?,?,?,?,?)");p Sql.setint (1, 3212);//Set parameter 1 to create data with ID 3212Psql.setstring (2, "Wang Gang");//Set parameter 2,name to Wang gangPsql.setstring (3, "President"); DateFormat DATEFORMAT2=NewSimpleDateFormat ("Yyyy-mm-dd");D ate myDate2= Dateformat2.parse ("2010-09-13"));p sql.setdate (4,Newjava.sql.Date (Mydate2.gettime ()));p Sql.setfloat (5, (float) 2000.3);p sql.executeupdate (); //Perform the update
Update data:
PreparedStatement psql; // preprocess Update (Modify) data, change the Wang Gang's sal to 5000.0psql = con.preparestatement ("update emp set sal =?"). where ename =? " );p sql.setfloat (1, (float) 5000.0); Psql.setstring (2, "Wang Gang"); Psql.executeupdate ();
Delete data:
PreparedStatement psql; // preprocessing Delete data psql = con.preparestatement ("Delete from emp where Sal >?") );p sql.setfloat (1, 4500);p sql.executeupdate ();p sql.close ();
Java connection MySQL database-with steps and code