JDBC 数据库连接 Java操作数据库 jdbc快速入门

JDBC基本概念 
     Java DataBase Connectivity  数据库连接  java操作数据库
本质上(sun公司的程序员)定义的一套操作关系型数据库的规则 既接口

 更新内容之前

代码

 

 1 package cn.zmh.JDBC;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.Statement;
 6 /*JDBC */
 7 public class Demo11JDBC {
 8     public static void main(String[] args) throws Exception {
 9         /* 1: 导入jar包 mysql-connector-java-5.1.37-bin
10                  1.1复制mysql-connector-java-5.1.37-bin到项目的libs目录下
11                  1.2  右键----》Add As Library
12          */
13         // 2 注册驱动
14         Class.forName("com.mysql.jdbc.Driver");
15         // 3 获取数据库连接对象  Connection
16         Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/qy97","root","root");
17         // 4 定义sql语句    UPDATE 表名 SET 列名='修改的字段' WHERE 键值
18         String sql1 = "UPDATE user1 SET unames='张三'   WHERE uid=1";
19         String sql2 = "UPDATE user1 SET unames='李四'   WHERE uid=2";
20         String sql3 = "UPDATE user1 SET unames='王五'   WHERE uid=3";
21         String sql4 = "UPDATE user1 SET unames='赵六'   WHERE uid=4";
22         String sql5 = "UPDATE user1 SET uaddress='北京' WHERE uid=1";
23         String sql6 = "UPDATE user1 SET uaddress='上海' WHERE uid=2";
24         String sql7 = "UPDATE user1 SET uaddress='广州' WHERE uid=3";
25         String sql8 = "UPDATE user1 SET uaddress='深圳' WHERE uid=4";
26         // 5 获得执行sql语句对象    Statement
27         Statement statement = connection.createStatement();
28         // 6 执行sql语句
29         int i1 = statement.executeUpdate(sql1);
30         int i2 = statement.executeUpdate(sql2);
31         int i3 = statement.executeUpdate(sql3);
32         int i4 = statement.executeUpdate(sql4);
33         int i5 = statement.executeUpdate(sql5);
34         int i6 = statement.executeUpdate(sql6);
35         int i7 = statement.executeUpdate(sql7);
36         int i8 = statement.executeUpdate(sql8);
37         // 7 控制台输出结果   可以不写
38         System.out.println(i1+""+i2+""+i3+""+i4+""+i5+""+i6+""+i7+""+i8);
39         // 8 释放资源
40         statement.close();
41         connection.close();
42 
43     }
44 }

 

更新内容之后

 

posted @ 2019-03-30 23:15  Java练习生  阅读(305)  评论(0编辑  收藏  举报