JDBC 实现 数据库的增删改查

柔情只为你懂 2022-09-24 14:18 339阅读 0赞

没有 JDBC和ODBC 之前,访问不同数据库需要编写相应的数据库访问程序,ODBC 使得访问链接数据库的接口统一,访问不同数据库的 API 一致。

JDBC 是 Java Database Connector,JDBC API 主要供开发人员使用,JDBC Driver API 主要供底层 数据库 开发人员使用,用于设计与具体数据库的接口。

JDBC 实现的流程

加载驱动 —— 加载数据库驱动,一般是Class.forName方法实现;

打开链接 —— 开启数据库链接,参数包括数据库 URL 、Name 和 pwd;

执行查询 —— 执行数据库查询语句;

处理结果 —— 处理数据库查询结果;

清理环境 —— 清理数据库环境,依次关闭开启的 API;

下面将展示数据库 MySQL 和 Eclipse 下的 数据库对数据的 增删改查代码

  1. create table tbl_user(
  2. id int(11) unsigned not null auto_increment,
  3. name varchar(50) not null default '',
  4. password varchar(50) not null default '',
  5. email varchar(50) default '',
  6. primary key (id))
  7. engine = InnoDB
  8. default charset = utf8;
  9. create table tbl_address(
  10. id int(11) unsigned not null auto_increment,
  11. city varchar(20) default null,
  12. country varchar(20) default null,
  13. user_id int(11) unsigned not null,
  14. primary key(id))
  15. engine = innodb
  16. default charset = utf8;
  17. insert into tbl_user(id,name,password,email) values
  18. (1,'xiaoming','123456','xiaoming@gmail.com'),
  19. (2,'xiangzhang','123456','xiaozhang@gmail.com');
  20. insert into tbl_address (city,country,user_id) values
  21. ('beijing','china',1);
  22. insert into tbl_address (city,country,user_id) values
  23. ('tianjin','china',2);
  24. package jdbc_Pack_003;
  25. import java.sql.Connection;
  26. import java.sql.DriverManager;
  27. import java.sql.ResultSet;
  28. import java.sql.Statement;
  29. /*
  30. jdbc 的执行顺序:
  31. 1.加载驱动
  32. 2.打开链接
  33. 3.执行查询
  34. 4.处理结果
  35. 5.清理环境
  36. */
  37. public class JDBC_Pack_003 {
  38. public static void main(String[] args) {
  39. String sql = "select * from tbl_user";
  40. Connection conn = null;
  41. Statement st = null;
  42. ResultSet rs = null;
  43. try {
  44. // Class 和其他类一样继承自 Obj 类,不同的是 它还封装了 被封装到 JVM 之中的类的信息,类的成员变量,
  45. // 类的成员方法,类的实现接口或者父类 等等
  46. // forName 用来初始化参数指定的类,并创建一个实例化对象
  47. // 此处的参数 是 jdbc 中的 MySQL 的驱动程序
  48. Class.forName("com.mysql.jdbc.Driver");
  49. // localhost 是服务器主机,3360 是主机端口,test 是DB的名字
  50. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","1233211234567");
  51. st = conn.createStatement();
  52. rs = st.executeQuery(sql);
  53. while (rs.next()) {
  54. System.out.print(rs.getInt("id")+" ");
  55. System.out.print(rs.getString("name")+" ");
  56. System.out.print(rs.getString("email")+" ");
  57. System.out.println();
  58. }
  59. } catch (Exception e) {
  60. // TODO: handle exception
  61. System.out.println(e.getMessage());
  62. }finally{
  63. try {
  64. rs.close();
  65. st.close();
  66. conn.close();
  67. } catch (Exception e) {
  68. // TODO: handle exception
  69. System.out.println(e.getMessage());
  70. }
  71. }
  72. }
  73. }
  74. package jdbc_Pack_004;
  75. import java.sql.Connection;
  76. import java.sql.DriverManager;
  77. import java.sql.Statement;
  78. public class JDBC_Cls_004 {
  79. public static Connection getConnection(){
  80. Connection connection = null;
  81. try{
  82. Class.forName("com.mysql.jdbc.Driver");
  83. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_db","root","1233211234567");
  84. }
  85. catch(Exception e){
  86. e.printStackTrace();
  87. }
  88. return connection;
  89. }
  90. public static void insert(){
  91. Connection connection = getConnection();
  92. try {
  93. String sql = "INSERT INTO tbl_user(name,password,email)VALUES('Tom','123456','tom@gmail.com')";
  94. Statement statement = (Statement) connection.createStatement();
  95. int count = statement.executeUpdate(sql);
  96. System.out.println("用户向表中插入了"+count+"条记录 !");
  97. connection.close();
  98. } catch (Exception e) {
  99. // TODO: handle exception
  100. e.printStackTrace();
  101. }
  102. }
  103. public static void update(){
  104. Connection connection = getConnection();
  105. try {
  106. String sql = "UPDATE tbl_user SET email='tom@hotmail.com' WHERE name='Tom'";
  107. Statement statement = (Statement) connection.createStatement();
  108. int count = statement.executeUpdate(sql);
  109. System.out.println("用户向表中更新了"+count+"条记录 !");
  110. connection.close();
  111. } catch (Exception e) {
  112. // TODO: handle exception
  113. e.printStackTrace();
  114. }
  115. }
  116. public static void delete(){
  117. Connection connection = getConnection();
  118. try {
  119. String sql = "DELETE FROM tbl_user WHERE name='Tom'";
  120. Statement statement = (Statement) connection.createStatement();
  121. int count = statement.executeUpdate(sql);
  122. System.out.println("用户向表中删除了"+count+"条记录 !");
  123. connection.close();
  124. } catch (Exception e) {
  125. // TODO: handle exception
  126. e.printStackTrace();
  127. }
  128. }
  129. public static void main(String []args) {
  130. // insert();
  131. // update();
  132. delete();
  133. }
  134. }

发表评论

表情:
评论列表 (有 0 条评论,339人围观)

还没有评论,来说两句吧...

相关阅读