The development tool for Eclipse, only the database driver is Mysql-connector-java-5.1.8-bin.jar
The first step is to refer to the MySQL driver in the project directory of Eclipse
Drive: https://dev.mysql.com/downloads/connector/j/
Select Build path and select Configure Build Path
Then select Libraries, select Add External Library, find the database driver, confirm, OK
Then the next test code drives the database.
Public class Test { publicstaticvoid main (String []args) { try { //MySQL Database setup driver type class.forname ("Com.mysql.jdbc.Driver"); System.out.println ("MySQL database driver loaded successfully"); } Catch (ClassNotFoundException e) { e.printstacktrace (); }}}
The driver code runs once, and then the database is operational.
Basic operation modification and deletion, the principle is the same
Create a Connection object first
NULL ; String User= "root"; // User name String password= "19980713"; // Password String url = "jdbc:mysql://localhost:3306/task?user=" +user+ "&password=" +password+ "&useUnicode=true& Characterencoding=utf-8 "; // get a connected statement Try { = (Connection) drivermanager.getconnection (URL); } Catch (SQLException e) { e.printstacktrace (); }
Once the connection is made, the connection is manipulated.
Query records
String sql = "Select id,name,job,department,state,authority from Worker;";Try{PreparedStatement PS=(PreparedStatement) connection.preparestatement (SQL);//Pre-compilation process ResultSet RS= Ps.executequery ();//Query general with ExecuteQuery, return isresult set of the query//here's how to work with the result set while(Rs.next ()) {count++; Vector v=NewVector (); V.add ((Rs.getstring (1))); V.add ((Rs.getstring (2))); V.add ((Rs.getstring (3))); V.add ((Rs.getstring (4))); V.add ((Rs.getstring (5))); V.add ((Rs.getstring (6))); Worker.add (v); } }Catch(SQLException E1) {e1.printstacktrace (); }
Inserting records
The rest of the operation is not affixed to the code, and so on, I first prepare a class six to go, will not wait to update
Basic methods of Java Operational database