当前位置:网站首页>Record the first JDBC
Record the first JDBC
2022-07-06 10:19:00 【Artificial intelligence cockroach】
Record the first JDBC
JDBC What are the
bring Java Connect to database ; Can pass java Drive different databases uniformly .
To configure JDBC
Guide pack
1. 1. 1. MySQL I use it 5.7.19, then maven Failed to import package , There is no corresponding one on the official website jar package , Finally, I saw a post saying ,5.7 Version of MySQL Direct use mysql-connector-java-5.1.34.jar The above jar Just a bag ;
2. 2. 2. Then import jar After package , Connect MySQL You need to set the time zone , Set to GMT Just ok 了 ;
3. 3. 3. After entering the user's password Click on test connection
4. 4. 4. Click on Schemas
I was then Schemas It's always empty , I found a lot of information and didn't solve it , The last restart idea Just fine ;
5. 5. 5. After successful connection , The database information can be displayed .
Insert a row
package com.tl.study01;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
/** * @author tl * Insert a row id value ( key ) Can't repeat */
public class TestJdbc02 {
public static void main(String[] args) throws Exception {
// Configuration information
//useUnicode=true&characterEncoding=utf-8 Solve the Chinese garbled code
String url="jdbc:mysql://localhost:3306/jdbc_study?" +
"useUnicode=true&characterEncoding=utf-8";
String username = "root";
String password = "123";
//1. The load driver
Class.forName("com.mysql.jdbc.Driver");
//2. Connect to database ,connection On behalf of the database
Connection connection = DriverManager.getConnection(url, username, password);
//3. To write SQL
String sql = "insert into users(id, name, password, email, birthday) values (?,?,?,?,?);";
//4. precompile ( Security )
PreparedStatement preparedStatement = connection.prepareStatement(sql);
// Give the first placeholder ? The value of is assigned to 4;
preparedStatement.setInt(1,4);
// Give the second placeholder ? The value of is assigned to root;
preparedStatement.setString(2,"root");
// Give the third placeholder ? The value of is assigned to 123;
preparedStatement.setString(3,"123");
// Give the fourth placeholder ? The value of is assigned to [email protected];
preparedStatement.setString(4,"[email protected]");
// Give the fifth placeholder ? The value of is assigned to new Date(new java.util.Date().getTime());
preparedStatement.setDate(5,new Date(new java.util.Date().getTime()));
//5. perform SQL
int i = preparedStatement.executeUpdate();
if (i>0){
System.out.println(" Insert the success @");
}
//6. Close the connection , Release resources ( Be sure to do ) Turn it on and off
preparedStatement.close();
connection.close();
}
}
Delete a line
package com.tl.study01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
/** * @author tl * Delete a line */
public class TestJdbc03 {
public static void main(String[] args) throws Exception {
// Configuration information
//useUnicode=true&characterEncoding=utf-8 Solve the Chinese garbled code
String url="jdbc:mysql://localhost:3306/jdbc_study?" +
"useUnicode=true&characterEncoding=utf-8";
String username = "root";
String password = "123";
//1. The load driver
Class.forName("com.mysql.jdbc.Driver");
//2. Connect to database ,connection On behalf of the database
Connection connection = DriverManager.getConnection(url, username, password);
//3. To write SQL
String sql = "delete from users where id =2";
//4. precompile ( Security )
PreparedStatement preparedStatement = connection.prepareStatement(sql);
int i = preparedStatement.executeUpdate(sql);
if (i>0){
System.out.println(" Delete successful @");
}
//6. Close the connection , Release resources ( Be sure to do ) Turn it on and off
preparedStatement.close();
connection.close();
}
}
Query table information
package com.tl.study01;
import java.sql.*;
/** * @author tl * Look up the table */
public class TestJdbc01 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//"useUnicode=true&characterEncoding=utf-8" Solve the Chinese garbled code
String url="jdbc:mysql://localhost:3306/jdbc_study?" +
"useUnicode=true&characterEncoding=utf-8";
String username = "root";
String password ="123";
// Drive by reflection loading
Class.forName("com.mysql.jdbc.Driver");
// Connect to database ,connection On behalf of the database
Connection connection = DriverManager.getConnection(url, username, password);
// Send to the database SQL The object of Statement:CRUD
Statement statement = connection.createStatement();// unsafe
// To write SQL Execute the query SQL, Return to one ResultSet: Result set
String sql ="select * from users";
ResultSet res = statement.executeQuery(sql);
while (res.next()){
System.out.println("id="+res.getObject("id"));
System.out.println("name="+res.getObject("name"));
System.out.println("password="+res.getObject("password"));
System.out.println("email="+res.getObject("email"));
System.out.println("birthday="+res.getObject("birthday"));
}
//6. Close the connection , Release resources ( Be sure to do ) Turn it on and off
res.close();
statement.close();
connection.close();
}
}
边栏推荐
- Control the operation of the test module through the panel in canoe (Advanced)
- Not registered via @enableconfigurationproperties, marked (@configurationproperties use)
- Several silly built-in functions about relative path / absolute path operation in CAPL script
- MySQL combat optimization expert 02 in order to execute SQL statements, do you know what kind of architectural design MySQL uses?
- Upload vulnerability
- 软件测试工程师必备之软技能:结构化思维
- UEditor国际化配置,支持中英文切换
- MySQL combat optimization expert 06 production experience: how does the production environment database of Internet companies conduct performance testing?
- Const decorated member function problem
- C miscellaneous lecture continued
猜你喜欢

Implement sending post request with form data parameter

ZABBIX introduction and installation

Sichuan cloud education and double teacher model

14 医疗挂号系统_【阿里云OSS、用户认证与就诊人】
![14 medical registration system_ [Alibaba cloud OSS, user authentication and patient]](/img/c4/81f00c8b7037b5fb4c5df4d2aa7571.png)
14 medical registration system_ [Alibaba cloud OSS, user authentication and patient]

Security design verification of API interface: ticket, signature, timestamp
![[after reading the series] how to realize app automation without programming (automatically start Kwai APP)](/img/e1/bad9cfa70d3c533cfaddeee40b96f1.jpg)
[after reading the series] how to realize app automation without programming (automatically start Kwai APP)

Docker MySQL solves time zone problems

Security design verification of API interface: ticket, signature, timestamp

Const decorated member function problem
随机推荐
C杂讲 文件 初讲
MySQL combat optimization expert 07 production experience: how to conduct 360 degree dead angle pressure test on the database in the production environment?
Embedded development is much more difficult than MCU? Talk about SCM and embedded development and design experience
Bugku web guide
MySQL实战优化高手12 Buffer Pool这个内存数据结构到底长个什么样子?
text 文本数据增强方法 data argumentation
华南技术栈CNN+Bilstm+Attention
Chrome浏览器端跨域不能访问问题处理办法
MySQL实战优化高手04 借着更新语句在InnoDB存储引擎中的执行流程,聊聊binlog是什么?
docker MySQL解决时区问题
Redis集群方案应该怎么做?都有哪些方案?
cmooc互联网+教育
The underlying logical architecture of MySQL
In fact, the implementation of current limiting is not complicated
Complete web login process through filter
Competition vscode Configuration Guide
Installation de la pagode et déploiement du projet flask
17 medical registration system_ [wechat Payment]
A new understanding of RMAN retention policy recovery window
Contest3145 - the 37th game of 2021 freshman individual training match_ C: Tour guide