码迷,mamicode.com
首页 > 数据库 > 详细

java链接oracle数据库

时间:2019-11-23 21:55:08      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:none   alt   display   gen   ORC   rom   state   加载驱动   catch   

技术图片
 1 package csu.lys.com;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 import java.util.Hashtable;
 9 
10 import javax.sql.StatementEvent;
11 
12 public class ConnOracle {
13 
14     /**
15      * @param args
16      */
17     public static void main(String[] args) {
18         // TODO Auto-generated method stub
19         //加载驱动
20         String oracleDriverString = "oracle.jdbc.driver.OracleDriver";
21         Hashtable<String, User> ht = new Hashtable<String, User>();
22         try {
23             //加载驱动
24             Class.forName(oracleDriverString);
25             //得到链接
26             Connection ct = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCLLYS", "SCOTT", "TIGER");
27             //创建statement
28             Statement statement = ct.createStatement();
29             //查询
30             ResultSet resultSet = statement.executeQuery("select * from users");
31             
32             if(resultSet.next()){
33                 int id = resultSet.getInt(1);
34                 String name = resultSet.getString(2);
35                 String passwd = resultSet.getString(3);
36                 User user = new User(id, name, passwd);
37                 ht.put("lys", user);
38             }
39             
40         } catch (Exception e) {
41             // TODO Auto-generated catch block
42             e.printStackTrace();
43         }
44         System.out.println(ht.get("lys").toString());
45 
46     }
47 
48 }
View Code

 

java链接oracle数据库

标签:none   alt   display   gen   ORC   rom   state   加载驱动   catch   

原文地址:https://www.cnblogs.com/yuezhihua/p/11919703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!