jdbc 增删改

Posted 西瓜的夏天

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdbc 增删改相关的知识,希望对你有一定的参考价值。

 1 package TommyPackage;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.SQLException;
 7 
 8 public class MyTest {
 9 
10     private static Connection getConn() {
11         String driver = "com.mysql.jdbc.Driver";
12         String url = "jdbc:mysql://sh-cdb-kljdeois.sql.tencentcdb.com:63014/yxm";
13         String username = "root";
14         String password = "j3s4d5f6";
15         Connection conn = null;
16         try {
17             Class.forName(driver); //classLoader,加载对应驱动
18             conn = DriverManager.getConnection(url, username, password);
19         } catch (ClassNotFoundException e) {
20             e.printStackTrace();
21         } catch (SQLException e) {
22             e.printStackTrace();
23         }
24         return conn;
25     }
26 
27     public static void main(String[] args) throws ClassNotFoundException, SQLException {
28         Connection con = getConn();
29         Person ps = new Person();
30 
31         ps.setLastName("Tommy");
32         ps.setFirstName("Lorry");
33         ps.setAddress("电信大楼");
34         ps.setCity("Shanghai");
35 
36         StringBuilder sb = new StringBuilder();
37         sb.append("‘" + ps.getLastName() + "‘");
38         sb.append(",");
39         sb.append("‘" + ps.getFirstName() + "‘");
40         sb.append(",");
41         sb.append("‘" + ps.getAddress() + "‘");
42         sb.append(",");
43         sb.append("‘" + ps.getCity() + "‘");
44 
45         if (con != null) {
46             System.out.println("mysql 连接成功!");
47 
48             //插入
49             String s = "" + "insert into Persons(LastName,FirstName,Address,City) values(" + sb.toString() + ")";
50             PreparedStatement pst1 = con.prepareStatement(s);
51             if (pst1.execute()) {
52                 System.out.println("插入数据成功!");
53             } else {
54                 System.out.println("插入数据失败!");
55             }
56 
57             //删除
58             String t = "delete from Persons where LastName=‘" + ps.getLastName() + "‘";
59             PreparedStatement pst2 = con.prepareStatement(t);
60             int i = pst2.executeUpdate();  //i返回删除的记录数
61             System.out.println("删除了: " + i + "条数据!");
62 
63             //修改
64             String Cityname = "London";
65             String u = "update Persons set LastName=‘" + ps.getLastName() + "‘ where City=‘" + Cityname + "‘";
66             PreparedStatement pst3 = con.prepareStatement(u);
67             int j = pst3.executeUpdate(); //j返回更新的记录数
68 
69             System.out.println("更新了: " + j + "条数据!");
70 
71             //关闭资源
72             pst1.close();
73             pst2.close();
74             pst3.close();
75             con.close();
76         }
77     }
78 }

 

以上是关于jdbc 增删改的主要内容,如果未能解决你的问题,请参考以下文章

java jdbc连接数据库 oracle的代码实现增删改查的方法怎么写

JDBC操作数据库--增删改查

用java jdbc sqlserver写登录注册,增删改查代码

JDBC增删改查和查唯一的完整代码

JDBC实现Mysqual的增删改查

通过JDBC进行简单的增删改查(以MySQL为例)