当前位置:网站首页>Initial JDBC programming
Initial JDBC programming
2022-07-02 12:09:00 【The dishes are not right】

Catalog
🥬JDBC The basic programming process
🥬 Specific operation
1、 open IDEA, Create a new project first

2、 Introduce dependencies ,JDBC Programming requires mysql Driver package .( The driver package is to mysq| Self api To convert into jdbc Style ). Download the driver package , Choose with yourself mysql The version corresponds to the version of the driver package .

3、 Import the driver package into the project
1) First create a directory in the new project just created

2) Download the jar Copy the file to the directory just created
3) Right click the directory just now , choice Add as library…

4、 Write code
🥬JDBC The basic programming process
1、 establish DataSource object , This object describes where the database server is .

Describe where the server is

2、 Let the code connect to the database server
Connection connection=dataSource.getConnection();
3、 Operate on the database
Take inserting data :
// The key is to construct a SQL sentence
String sql="insert into student values(1,“ Zhang San ”)";
// Here is just a String Type of sql Not yet , You need to put this String Pack it as a " Statement object "
PreparedStatement statement=connection.prepareStatement(sql);
4、 perform SQL
// SQL If it's insert, update, delete, All use executeUpdate Method .
// SQL If it's select, Then use executeQuery Method .
// The return value indicates the operation , Affected several lines .
int ret= statement.executeUpdate();
System.out.println(ret);5、SQL After execution , Release resources
// Create before release
statement.close();
connection.close();The specific code is as follows :
public class TestJDBC {
public static void main(String[] args) throws SQLException {
Scanner scanner=new Scanner(System.in);
//1、 create data source
DataSource dataSource=new MysqlDataSource();
// Set the location of the database
((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/testdemo?characterEncoding=utf8&useSSL=false");
// Set the user name to log in to the database
((MysqlDataSource) dataSource).setUser("root");
// Set the password to log in to the database
((MysqlDataSource) dataSource).setPassword("123456");
//2、 Let the code establish a connection with the database server
Connection connection=dataSource.getConnection();
//2.1 Input data from the console
System.out.println(" Please enter the student number :");
int id=scanner.nextInt();
System.out.println(" Please enter a name ");
String name=scanner.next();
//3、 Operating the database , Take inserting data
// The key is to construct a SQL sentence
String sql="insert into student values(?,?)";
//? It's equivalent to occupying a position , The values of these two fields are still uncertain , The following is used PrepareStatement Medium setxxx Replace with a series of methods
// Here is just a String Type of sql Not yet , You need to put this String Pack it as a " Statement object "
PreparedStatement statement=connection.prepareStatement(sql);
// Carry out replacement operations
statement.setInt(1,id);
statement.setString(2,name);
System.out.println("statement:"+statement);
// 4、 perform SQL
// SQL If it's insert, update, delete, All use executeUpdate Method .
// SQL If it's select, Then use executeQuery Method .
// The return value indicates the operation , It's affecting A few lines . It's equivalent to typing... In the console sql after , The numbers you get
int ret= statement.executeUpdate();
System.out.println(ret);
//5、 here SQL completion of enforcement , Need to release resources
// Create before release
statement.close();
connection.close();
}
}🥬 Summary
That's what we have today , If you have any questions, you can leave a message in the comment area

边栏推荐
- (C language) octal conversion decimal
- PyTorch搭建LSTM实现服装分类(FashionMNIST)
- lombok常用注解
- Dynamic debugging of multi file program x32dbg
- H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
- Repeat, tile and repeat in pytorch_ The difference between interleave
- 求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
- 史上最易懂的f-string教程,收藏这一篇就够了
- HOW TO ADD P-VALUES TO GGPLOT FACETS
- CDA数据分析——Excel数据处理的常见知识点归纳
猜你喜欢

Map and set

(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.

Jenkins用户权限管理

How to Add P-Values onto Horizontal GGPLOTS
![[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)](/img/18/f0c9ef6250a717f8e66c95da4de08c.jpg)
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)

5g era, learning audio and video development, a super hot audio and video advanced development and learning classic

CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节

ES集群中节点与分片的区别

Yygh-9-make an appointment to place an order

自然语言处理系列(二)——使用RNN搭建字符级语言模型
随机推荐
Fabric. JS 3 APIs to set canvas width and height
MySQL indexes and transactions
基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
JZ63 股票的最大利润
Log4j2
Leetcode209 长度最小的子数组
CDH6之Sqoop添加数据库驱动
CDA数据分析——Excel数据处理的常见知识点归纳
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
Leetcode topic [array] -540- single element in an ordered array
Larvel modify table fields
Jenkins user rights management
H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
Brush questions --- binary tree --2
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
Le tutoriel F - String le plus facile à comprendre de l'histoire.
b格高且好看的代码片段分享图片生成
uniapp uni-list-item @click,uniapp uni-list-item带参数跳转
drools决策表的简单使用
小程序链接生成