- Project description
The project uses the MAVEN organization, and the only dependency of JDBC is the Mysql-connector-java pom dependency as follows:
<Dependency><groupId>Mysql</groupId><Artifactid>Mysql-connector-java</Artifactid><version>5.1.30</version></Dependency></Dependencies>
2 Project Structure
3 DB Script
DROP TABLE IF EXISTS' account ';CREATE TABLE' account ' (' ID ')int( One) not NULLauto_increment, ' name 'varchar( $)DEFAULT NULL,` Money`decimal(Ten,0)DEFAULT NULL,PRIMARY KEY(' id ')) ENGINE=InnoDB auto_increment=103 DEFAULTCHARSET=UTF8;INSERT into' Account 'VALUES('1','hbb0b0','40000');INSERT into' Account 'VALUES('2','Kael',' +');
4 Java Code
Packagehbb0b0.jdbc01;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.Statement;Importcom.mysql.jdbc.PreparedStatement;/*** app**/ Public classApp {//DB URLPrivate Static FinalString URL = "Jdbc:mysql://localhost:3306/study";Private Static FinalString NAME = "root";Private Static FinalString PASSWORD = "Sqlsa"; Public Static voidMain (string[] args)throwsException {class.forname ("Com.mysql.jdbc.Driver"); //connecting to a databaseConnection conn =drivermanager.getconnection (URL, NAME, PASSWORD);//construct an INSERT statementPreparedStatement PSMT =(PreparedStatement) conn.preparestatement (Insert account (Name,money) VALUES (?,?)); /*Insert*/ for(inti = 0; I < 100; i++) {//parameter AssignmentPsmt.setstring (1, String.Format ("hbb0b0-%d", i)); Psmt.setdouble (2, i);//Execute INSERT statementBoolean result =Psmt.execute ();//INSERT statement Execution resultSystem.out.println (result);} //constructing a SELECT statementStatement stmt =conn.createstatement ();//Execute SELECT statementResultSet rs = stmt.executequery ("Select Id,name,money from Account"); //Output Query Structure while(Rs.next ()) {System.out.println (Rs.getint ("id") + "," + rs.getstring ("name"))+ "," + rs.getdouble ("Money")));} }}
Java DB Access JDBC