当前位置:网站首页>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
边栏推荐
猜你喜欢
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
Sort---
HOW TO ADD P-VALUES TO GGPLOT FACETS
Power Spectral Density Estimates Using FFT---MATLAB
How to Add P-Values onto Horizontal GGPLOTS
Take you ten days to easily finish the finale of go micro services (distributed transactions)
Mish shake the new successor of the deep learning relu activation function
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
Jenkins用户权限管理
YYGH-BUG-04
随机推荐
求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
5g era, learning audio and video development, a super hot audio and video advanced development and learning classic
深入理解PyTorch中的nn.Embedding
Leetcode122 买卖股票的最佳时机 II
记录一下MySql update会锁定哪些范围的数据
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
Codeforces 771 div2 B (no one FST, refers to himself)
On data preprocessing in sklearn
堆(优先级队列)
自然语言处理系列(一)——RNN基础
Maximum profit of jz63 shares
机械臂速成小指南(七):机械臂位姿的描述方法
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
Sort---
Natural language processing series (I) -- RNN Foundation
YYGH-BUG-04
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
Implementation of address book (file version)