1 package com.hanqi.test;
 2 
 3 import java.sql.*;
 4 
 5 public class jdbcTest {
 6 
 7     public static void main(String[] args) {
 8 
 9         // 测试JDBC
10 
11         Connection conn = null;
12 
13         try {
14 
15             // 1.加载驱动和注册
16             // 每一种数据库提供不同的驱动名
17             Class.forName("oracle.jdbc.driver.OracleDriver");// 加载
18 
19             // 2.连接数据库 :用户名,密码,URL数据库地址
20             // url-
21             String strUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
22 
23             // 得到连接
24             conn = DriverManager.getConnection(strUrl, "test1", "1234");
25 
26             System.out.println("连接数据库成功");
27 
28             // 3.操作数据库,增删改查
29             //执行sql语句完成响应的功能
30             
31             Statement st = conn.createStatement();
32             
33             //执行增删改语句
34             int i = st.executeUpdate("update T_students set sname = '小四' where sno = '101'");
35             if( i > 0 )
36             {
37                 System.out.println("操作数据库成功,影响了"+i+"+数据");
38             }
39             else
40             {
41                 System.out.println("操作数据库失败");
42             }
43             
44             st.close();
45 
46             // 4.关闭连接
47         }
48 
49         catch (Exception e)
50 
51         {
52 
53             e.printStackTrace();
54             System.out.println("连接数据库失败");
55         } finally {
56             if (conn != null) {
57                 try {
58                     conn.close();
59                 } catch (SQLException e) {
60                     // TODO 自动生成的 catch 块
61                     e.printStackTrace();
62                 }
63             }
64         }
65     }
66     
67 
68 }

 

posted on 2016-06-20 10:12  周帝  阅读(340)  评论(0编辑  收藏  举报