Java DB Access JDBC

Source: Internet
Author: User
Tags stmt

    1. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.