当前位置: 移动技术网 > IT编程>开发语言>Java > JDBC编程:使用 Statement 修改数据库

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

2018年08月19日  | 移动技术网IT编程  | 我要评论

  获取数据连接后,即可对数据库中的数据进行修改和查看。使用 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 }

 

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网