Sit tight, the old driver is on the grid.
First, create a new database in MySQL, named JDBC.
Then create a new table called person in JDBC,
Adding elements to a table
Database and tables have been built, and then connected,
Create a new project, right key project name create a new folder called Lib, and then copy the database driver,
The landlord is the good man, the database drives in this: Http://pan.baidu.com/s/1o8Qwco6
Right-click on the added driver, bulid bath, then add bath, the first one that appears after the build, then the database driver is ready:
The rest is the Code section:
Package com.it.test;
Import java.sql.Connection;
Import Java.sql.Driver;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.util.Properties;
Import java.sql.*;
Import com.mysql.jdbc.PreparedStatement;
public class Connection {static String url= ("Jdbc:mysql://localhost:3306/jdbc"); JDBC Protocol: Database Child Protocol://HOST: Port/Database to link to static string user = "root";//username static string password = "zhao123456";//password, please write your own password @S Uppresswarnings ("null") public static Connection test () throws SQLException {Driver drive=new com.mysql.jdbc.Driver ( ///Create driver object Properties Pro = new properties ()//Set username and password Pro.setproperty ("user", user);//Account Pro.setproperty ("Passwo Rd ", password);//password Connection con = drive.connect (URL, Pro)//Connection database, return object System.out.println (Con);
Con public static void Main (string[] args) throws SQLException {//TODO auto-generated method Stub Connection con =test ();//Connect the database and test String sql = "SELECT * from person";//sql statement, query table PreparEdstatement pstmt=null;//Create statement ResultSet Rs=null;
pstmt= (PreparedStatement) con.preparestatement (SQL);
rs = Pstmt.executequery ();//rs for the queried object//system.out.println (RS);
while (Rs.next ()) {System.out.println (rs.getstring ("name") + "" +rs.getstring ("sex") + "" +rs.getstring ("Age"));
} System.out.println ("-------update-----------");
String sql1= "UPDATE person SET sex= ' woman ' WHERE age = 18";
String sex= "female";
System.out.println ("1");
String age = "18";
Pstmt.setstring (1,sex);
Pstmt.setint (1, 18);
Pstmt.setstring (1, "");
System.out.println ("1");
Pstmt.setstring (2, age); pstmt= (PreparedStatement) con.preparestatement (SQL1);//Load UPDATE statement pstmt.executeupdate ();//update String SQL2 = "SELECT *
From person ";
pstmt= (PreparedStatement) con.preparestatement (SQL2)//Load query statement ResultSet rs1=pstmt.executequery ();
while (Rs1.next ()) {System.out.println (rs1.getstring ("name") + "" +rs1.getstring ("sex") + "" +rs1.getstring ("Age")); } SYstem.out.println ("-------add-----------");
PreparedStatement Pstmt1=null;
String sql3= "INSERT into person VALUES (' Zhao Liu ', ' men ', 19)"; pstmt1= (PreparedStatement) con.preparestatement (SQL3);//Mount Add statement pstmt1.executeupdate ();//Execute String sql6= "SELECT *
From person ";
pstmt1= (PreparedStatement) con.preparestatement (SQL6)//Load Query statement//pstmt1.executeupdate ();//Execute, query statement cannot update .... ResultSet rs2=pstmt1.executequery ()//Get while (Rs2.next ()) {System.out.println (rs2.getstring ("name") + "" +rs2.getst
Ring ("sex") + "" +rs2.getstring ("Age"));
} System.out.println ("-------delete-----------");
String sql4= "DELETE from person WHERE age=19"; pstmt1= (PreparedStatement) con.preparestatement (SQL4)//Mount DELETE statement pstmt1.executeupdate ();//execute String SQL5 = "SELECT * Fro
M person "; pstmt1= (PreparedStatement) con.preparestatement (SQL5)//Load Query statement//pstmt1.executeupdate ();//execute ResultSet rs3= Pstmt1.executequery ()//Get while (Rs3.next ()) {System.out.println (rs3.getstring ("name") + "" +rs3.getstring("Sex") + "" +rs3.getstring ("Age"));
Pstmt.close ();//close Execute SQL statement pstmt1.close ();
Con.close ();//Close the database connection}}
The results of the execution are:
Finally, the query statement, there is no this sentence//pstmt1.executeupdate ();//UPDATE statement, query statement cannot update ....
If you add this sentence, this exception appears:
Exception in thread ' main ' java.sql.SQLException:Can not issue executeupdate () for selects
At Com.mysql.jdbc.SQLError.createSQLException (sqlerror.java:1055)
At Com.mysql.jdbc.SQLError.createSQLException (sqlerror.java:956)
At Com.mysql.jdbc.SQLError.createSQLException (sqlerror.java:926)
At Com.mysql.jdbc.PreparedStatement.executeUpdate (preparedstatement.java:2004)
At Com.mysql.jdbc.PreparedStatement.executeUpdate (preparedstatement.java:1964)
At Com.mysql.jdbc.PreparedStatement.executeUpdate (preparedstatement.java:1949)
At Com.it.test.connection.main (connection.java:79)
Query statement not updated ....