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

边栏推荐
- On data preprocessing in sklearn
- drools决策表的简单使用
- H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
- 还不会安装WSL 2?看这一篇文章就够了
- Addition, deletion, modification and query of MySQL table (Advanced)
- How to Create a Beautiful Plots in R with Summary Statistics Labels
- This article takes you to understand the operation of vim
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
- Differences between nodes and sharding in ES cluster
- SCM power supply
猜你喜欢

Yygh-9-make an appointment to place an order

Map和Set

MySQL indexes and transactions

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

Dynamic memory (advanced 4)

Sort---

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

Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law

How to Easily Create Barplots with Error Bars in R

HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
随机推荐
jenkins 凭证管理
初始JDBC 编程
HR wonderful dividing line
Those logs in MySQL
drools决策表的简单使用
Leetcode739 daily temperature
MSI announced that its motherboard products will cancel all paper accessories
GGPlot Examples Best Reference
Jenkins voucher management
输入一个三位的数字,输出它的个位数,十位数、百位数。
FastDateFormat为什么线程安全
甜心教主:王心凌
Natural language processing series (II) -- building character level language model using RNN
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
Jenkins user rights management
Pytorch builds LSTM to realize clothing classification (fashionmnist)
From scratch, develop a web office suite (3): mouse events
Larvel modify table fields
Lombok common annotations
How does Premiere (PR) import the preset mogrt template?