首页 > 数据库技术 > 详细

JDBC编程:使用 Statement 修改数据库

时间:2018-08-19 15:20:01      阅读:178      评论:0      收藏:0      [点我收藏+]

  获取数据连接后,即可对数据库中的数据进行修改和查看。使用 Statement 接口可以对数据库中的数据进行修改,下面是程序演示。

 

 1 /**
 2  * 获取数据库连接,并使用SQL语句,向数据库中插入记录
 3  */
 4 package com.pack03;
 5 
 6 import java.io.InputStream;
 7 import java.sql.Connection;
 8 import java.sql.DriverManager;
 9 import java.sql.SQLException;
10 import java.sql.Statement;
11 import java.util.Properties;
12 
13 public class TestStatement {
14 
15     //***************************该方法用于获取数据库连接*****************************
16     public static Connection getConnection() throws Exception {
17         // 1.将配置文件中的连接信息获取到Properties对象中
18         InputStream is = 
19                 TestStatement.class.getClassLoader().getResourceAsStream("setting.properties");
20 
21         Properties setting = new Properties();
22         setting.load(is);
23 
24         // 2.从Properties对象中读取需要的连接信息
25         String driverName = setting.getProperty("driver");
26         String url = setting.getProperty("url");
27         String user = setting.getProperty("user");
28         String password = setting.getProperty("password");
29 
30         // 3.加载驱动程序,即将数据库厂商提供的Driver接口实现类加载进内存;
31         // 该驱动类中的静态代码块包含有注册驱动的程序,在加载类时将被执行
32         Class.forName(driverName);
33 
34         // 4.通过DriverManager类的静态方法getConnection获取数据连接
35         Connection conn = DriverManager.getConnection(url, user, password);
36         
37         return conn;
38     }
39     
40     
41     //************************该方法用于执行SQL语句,修改数据库内容*************************
42     public static void testStatement( String sqlStatement ) {
43         
44         Connection conn = null;
45         Statement statement = null;
46         
47         try {
48             //1.获取到数据库的连接
49             conn = getConnection();
50             
51             //2.用Connection中的 createStatement()方法获取 Statement 对象
52             statement = conn.createStatement();
53             
54             //3.调用 Statement 对象的 executeUpdate()方法,执行SQL语句并修改数据库
55             statement.executeUpdate( sqlStatement );
56             
57         } catch (Exception e) {
58             
59             e.printStackTrace();
60             
61         } finally {
62             
63             //4.关闭Statement对象
64             if(statement != null) {
65                 try {
66                     statement.close();
67                 } catch (SQLException e) {
68                     e.printStackTrace();
69                 }
70             }
71             
72             //5.关闭 Connection对象
73             if(conn != null) {
74                 try {
75                     conn.close();
76                 } catch (SQLException e) {
77                     e.printStackTrace();
78                 }
79             }
80         }
81     }
82     
83     public static void main(String[] args) {
84         
85         
86         String sqlInsert = "insert into tab001 values( 3, ‘小明3‘ )"; //插入语句
87         String sqlUpdate = "update tab001 set name=‘王凯‘ where id=1"; //修改语句
88         String sqlDelete = "delete from tab001 where id=2"; //删除语句
89         //对于Statement对象,不能执行select语句
90         
91         testStatement( sqlInsert );
92         testStatement( sqlUpdate );
93         testStatement( sqlDelete );
94     }
95 }

 

注:希望与各位读者相互交流,共同学习进步。

JDBC编程:使用 Statement 修改数据库

原文:https://www.cnblogs.com/EarthPioneer/p/9501269.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!