基础JDBC连接与使用教程

Jdbc(Java DataBase Connectivity), 是一种JAVA数据库连接方法。

实现步骤

1.加载驱动(先引入mysql的jar包)
2.创建连接
3.编写sql语句
4.获得statement对象(用来接收sql语句)
5.执行sql语句获得结果
6.结果处理
7.关闭资源

具体代码

先创建一个tb_user表(id,username,password)

查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.JDBC;

import java.sql.*;
//数据库查询
public class find {
public static void main(String[] args) {
try{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//创建连接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/train","root","123456");
System.out.println("创建连接成功");
//写sql语句
String sql="select * from tb_user";
//获得statement对象
PreparedStatement statement = connection.prepareStatement(sql);
//执行sql得到结果
ResultSet resultSet = statement.executeQuery();
//处理结果
while (resultSet.next()){
System.out.println(resultSet.getInt(1));
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getString(3));
}
//关闭资源
DBUtil.closeAll(resultSet,statement,connection);
}catch (Exception e){
System.out.println(e);
}
}
}

添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class add {
public static void main(String[] args) {
try{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//创建连接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/train","root","123456");
System.out.println("创建连接成功");
//写sql语句
String sql="insert tb_user values(null,'whb','785')";
//获得statement对象
PreparedStatement statement = connection.prepareStatement(sql);
//执行sql得到结果
statement.executeUpdate();
//处理结果
//关闭资源
DBUtil.closeAll(null,statement,connection);
}catch (Exception e){
System.out.println(e);
}

}
}

删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.JDBC;

import java.sql.*;

public class Delete {
public static void main(String[] args) {
try{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//创建连接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/train","root","123456");
System.out.println("创建连接成功");
//写sql语句
String sql="delete from tb_user where id = 2";
//获得statement对象
PreparedStatement statement = connection.prepareStatement(sql);
//执行sql得到结果
statement.executeUpdate();
//处理结果

//关闭资源
DBUtil.closeAll(null,statement,connection);
}catch (Exception e){
System.out.println(e);
}

}
}

修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Change {
public static void main(String[] args) {
try{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//创建连接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/train","root","123456");
System.out.println("创建连接成功");
//写sql语句
String sql="UPDATE tb_user set password = 423 where id=1";
//获得statement对象
PreparedStatement statement = connection.prepareStatement(sql);
//执行sql得到结果
statement.executeUpdate();
//处理结果
//关闭资源
DBUtil.closeAll(null,statement,connection);
}catch (Exception e){
System.out.println(e);
}

}
}

关闭方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.JDBC;

import java.sql.*;

public class DBUtil {
// public static Connection getConnection() throws ClassNotFoundException, SQLException {
// //加载驱动
// Class.forName("com.mysql.jdbc.Driver");
// //创建连接
// Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/train","root","123456");
// System.out.println("创建连接成功");
// return connection;
// }
public static void closeAll(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {
if (resultSet!=null){
resultSet.close();
}
if (statement!=null){
statement.close();
}
if (connection!=null){
connection.close();
}

}
}

简单易学,谢谢观看!