我是靠谱客的博主 聪慧鞋垫,这篇文章主要介绍最原始JDBCJDBC,现在分享给大家,希望可以做个参考。

提示:

JDBC

  • JDBC
    • JDBC的简单步骤
    • JDBC的通用模板


JDBC

JDBC: java datebase connectivity java连接数据库技术。
JDBC: SUN公司提供的一套专门用来实现java语言连接不同的数据库的标准接口

==不同的数据库厂商想要让java语言连接访问自己的数据库,就要去遵循JDBC规范,去实现所有的接口,并提供该接口的类的jar包,该包就是数据库驱动

mysql的数据库驱动jar包链接:https://pan.baidu.com/s/12OA_FqMrE17xR6jZifWn3w
提取码:xxal
包:
java.sql.*;

JDBC的简单步骤

复制代码
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class Test1 { public static void main(String[] args) throws ClassNotFoundException, SQLException { // 00 下载mysql的数据库驱动包,把包导入到当前的项目 // jar包右键--> build path---> add to build path // 1,加载驱动 // mysql驱动包 5版: com.mysql.jdbc.Driver // mysql驱动包 8版: com.mysql.cj.jdbc.Driver Class.forName("com.mysql.jdbc.Driver"); // 2,连接连接 // http://ip:端口号/资源路径 // mysq驱动 5版 jdbc:mysql://localhost:3306/数据库名 // mysql 8版 驱动 5版 jdbc:mysql://localhost:3306/ss?characterEncoding=gbk // mysq驱动 8版 jdbc:mysql://localhost:3306/数据库名?serverTimezone=UTC String url = "jdbc:mysql://localhost:3306/employee"; Connection connection = DriverManager.getConnection(url, "root", "root"); //System.out.println(connection); // 3,创建sql对象 //String sql = "insert into student values(7,'田七','男','1998-2-3',23)"; //String sql = "update student set name='田七七' where id=7 "; Statement sta = connection.createStatement(); // 4,执行sql语句 String sql = "delete from student where id=7"; int num = sta.executeUpdate(sql); // executeUpdate() 增加 删除 修改 // 5,处理结果集 if(num==0) { System.out.println("操作失败"); }else { System.out.println("操作成功"); } // 6,释放资源 sta.close(); connection.close(); } }

JDBC的通用模板

下边模板复制即可使用
记得先导入jar包

复制代码
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package JDBCUtil; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class jdbcUtil { private static String driver = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql:///duobiao"; private static String root = "root"; private static String password = "980214"; private static Connection connection = null; private static PreparedStatement prepareStatement = null; private static ResultSet executeQuery=null; /* * 加载驱动 */ static { try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /* * 建立连接 */ public static void myconnection() { try { connection = DriverManager.getConnection(url,root,password); } catch (SQLException e) { e.printStackTrace(); } } /* *增删改 */ public static int update(String sql,Object[] object) { myconnection(); int executeUpdate=0; try { prepareStatement = connection.prepareStatement(sql); if (object!=null) { for (int i = 0; i < object.length; i++) { prepareStatement.setObject(i+1, object[i]); } } executeUpdate = prepareStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally { close(); } return executeUpdate; } /* * 查询 */ public static ResultSet select(String sql,Object[] object) { myconnection(); try { prepareStatement = connection.prepareStatement(sql); if (object!=null) { for (int i = 0; i < object.length; i++) { prepareStatement.setObject(i+1, object[i]); } } executeQuery = prepareStatement.executeQuery(); }catch (Exception e) { } return executeQuery; } // 释放资源 public static void close() { try { if (prepareStatement!=null) { prepareStatement.close(); } if (connection!=null) { connection.close(); } if (executeQuery!=null) { executeQuery.close(); } } catch (SQLException e) { e.printStackTrace(); } } }

最后

以上就是聪慧鞋垫最近收集整理的关于最原始JDBCJDBC的全部内容,更多相关最原始JDBCJDBC内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(74)

评论列表共有 0 条评论

立即
投稿
返回
顶部