当前位置:网站首页>Quick start JDBC
Quick start JDBC
2022-07-29 04:54:00 【Chen Yuchen】
Quick start JDBC
1. The steps of writing code
Create a project , Import mysql Connected driver jar package
Under the project under construction src Create a special storage driver in the directory jar The folder of the package – lib Local jar Copy to this directory
take jar Add package as library file , Here's the picture :

Registration drive
Tips :1.MySQL 5 Later driver package , You can omit the steps of registering the driver
2. Automatic loading jar In bag META-INF/services/java.sql.Driver Driver class in file
Class.forName("com.mysql.jdbc.Driver");
- Get the connection
Java The code needs to be sent SQL to MySQL Server side , You need to establish a connection first
String url = "jdbc:mysql://127.0.0.1:3306/db1";
String username = "root";
String password = "1234";
Connection conn = DriverManager.getConnection(url, username, password);
Definition SQL sentence
Define a String Data of type , Initialize to SQL sentence , Some operations on the database .( Additions and deletions )Get the executed SQL sentence
perform SQL Sentence needs SQL Perform object , And the execution object is Statement object
Statement stmt = conn.createStatement();
- perform SQL sentence
stmt.executeUpdate(sql);// Return to one int Data of data type , Indicates the number of rows affected after execution
Release resources
Return processing results
Sample code implementation :
/** * JDBC Quick start */
public class JDBCDemo {
public static void main(String[] args) throws Exception {
//1. Registration drive
//Class.forName("com.mysql.jdbc.Driver");
//2. Get the connection
// If you access the local database locally , Can be abbreviated jdbc:mysql:/// Database name ? Parameter key value pair
String url = "jdbc:mysql://127.0.0.1:3306/db1";
String username = "root";
String password = "1234";
Connection conn = DriverManager.getConnection(url, username, password);
//3. Definition sql
String sql = "update account set money = 2000 where id = 1";
//4. Access to perform sql The object of Statement
Statement stmt = conn.createStatement();
//5. perform sql
int count = stmt.executeUpdate(sql);// Rows affected
//6. Processing results
System.out.println(count);
//7. Release resources
stmt.close();
conn.close();
}
}
边栏推荐
- Conv1d of torch
- How to debug UDP port
- ios面试准备 - objective-c篇
- On prepayment of house purchase
- Auto.js脚本开发入门
- Connection database time zone setting
- def fasterrcnn_resnet50_fpn()实例测试
- How to monitor micro web services
- Software test interview questions (4)
- Solution to the fourth game of 2022 Hangzhou Electric Multi school league
猜你喜欢

Five correlation analysis, one of the most important skills of data analysts

新产品上市最全推广方案

Use more flexible and convenient Rogowski coil

Traffic flow prediction pit climbing record (I): traffic flow data set, original data

输入的查询SQL语句,是如何执行的?

Sguard64.exe ace guard client exe: frequent disk reading and writing, game jamming, and Solutions

常见的限流方式

SparkSql批量插入或更新,保存数据到Mysql中

mujoco和mujoco_py安装以及解决libXcursor.so.1:NO such dictionary

Stack and queue and priority queue (large heap and small heap) simulation implementation and explanation of imitation function
随机推荐
un7.28:redis客户端常用命令。
DataSourceClosedException: dataSource already closed at Mon Oct 25 16:55:48 CST 2021
Deep analysis of data storage in memory (Advanced C language)
Using jupyter (I), install jupyter under windows, open the browser, and modify the default opening address
Improve the readability of your regular expressions a hundred times
Introduction to auto.js script development
让你的正则表达式可读性提高一百倍
The most complete NLP Chinese and English stop words list in the whole station (including punctuation marks, which can be copied directly)
iOS面试准备 - ios篇
数据湖:分布式开源处理引擎Spark
Torch.nn.crossentropyloss() details
EMI interference troubleshooting with near-field probe and current probe
Basic grammar of C language
如何安装office2010安装包?office2010安装包安装到电脑上的方法
Simple user-defined authentication interface rules
Traffic flow prediction pit climbing record (I): traffic flow data set, original data
Auto.js脚本开发环境搭建
1 句代码,搞定 ASP.NET Core 绑定多个源到同一个类
盒子水平垂直居中布局(总结)
(heap sort) heap sort is super detailed, I don't believe you can't (C language code implementation)