当前位置:网站首页>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

边栏推荐
- Le tutoriel F - String le plus facile à comprendre de l'histoire.
- Deep understanding of NN in pytorch Embedding
- How to Create a Beautiful Plots in R with Summary Statistics Labels
- drools执行指定的规则
- The most understandable f-string tutorial in history, collecting this one is enough
- ThreadLocal的简单理解
- Post request body content cannot be retrieved repeatedly
- CDA数据分析——AARRR增长模型的介绍、使用
- (C语言)八进制转换十进制
- drools执行String规则或执行某个规则文件
猜你喜欢

自然语言处理系列(一)——RNN基础

Larvel modify table fields

drools动态增加、修改、删除规则

K-Means Clustering Visualization in R: Step By Step Guide

GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL

CDH6之Sqoop添加数据库驱动

Natural language processing series (II) -- building character level language model using RNN

MSI announced that its motherboard products will cancel all paper accessories

堆(优先级队列)

How does Premiere (PR) import the preset mogrt template?
随机推荐
Dynamic debugging of multi file program x32dbg
CONDA common command summary
How to Create a Beautiful Plots in R with Summary Statistics Labels
[C language] convert decimal numbers to binary numbers
Leetcode209 长度最小的子数组
Filtre de profondeur de la série svo2
堆(优先级队列)
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
(C语言)八进制转换十进制
How to Visualize Missing Data in R using a Heatmap
On data preprocessing in sklearn
WSL 2 will not be installed yet? It's enough to read this article
PyTorch搭建LSTM实现服装分类(FashionMNIST)
Dynamic memory (advanced 4)
File operation (detailed!)
CDA数据分析——Excel数据处理的常见知识点归纳
输入一个三位的数字,输出它的个位数,十位数、百位数。
Map and set
Yygh-9-make an appointment to place an order
Maximum profit of jz63 shares