1

 1 public class JdbcUtils {
 2      private static final String driverUrl = "oracle.jdbc.driver.OracleDriver";  
 3       
 4         private static final String url = "jdbc:oracle:thin:@localhost:1521:Orcl";  
 5           
 6         private static final String username = "scott";  
 7           
 8         private static final String password = "Orcl123456";  
 9           
10         public static Connection getConnection(){  
11             Connection connection = null;  
12             try {  
13                 Class.forName(driverUrl);  
14                 connection = DriverManager.getConnection(url, username, password);  
15             } catch (ClassNotFoundException e) {  
16                 e.printStackTrace();  
17             } catch (SQLException e) {  
18                 e.printStackTrace();  
19             }  
20             return connection;  
21         }  
22           
23         // 测试Oracle连接是否成功  
24         public static void main(String[] args) {  
25             Connection connection = JdbcUtils.getConnection();  
26             List<UserInfo> list = new ArrayList<UserInfo>();
27 
28             try {
29                 Statement statement = connection.createStatement();
30                 ResultSet rs = statement.executeQuery("select * from SCOTT.\"userinfo\"");
31                 while(rs.next()){
32                     int id = rs.getInt(1);
33                     String name = rs.getString(2);
34                     String password = rs.getString(3);
35                     int sex = rs.getInt(4);
36                     String gif = rs.getString(5);
37                     Date date = rs.getDate(6);
38                     int type = rs.getInt(7);
39                     UserInfo info = new UserInfo();
40                     info.setUid(id);
41                     info.setUname(name);
42                     info.setUpassword(password);
43                     info.setUsex(sex==0?false:true);
44                     info.setUface(gif);
45                     info.setUregtime(date);
46                     info.setUtype(type);
47                     list.add(info);
48                 }
49                 
50                 
51             } catch (SQLException e) {
52                 e.printStackTrace();
53             }
54             for(UserInfo info:list){
55                 System.out.println(info.getUname());
56             }
57             System.out.println("连接成功:"+connection);  
58         }  
59 }

 

posted on 2017-08-22 16:37  Sharpest  阅读(287)  评论(0编辑  收藏  举报