java连接oracle以及Mysql

java连接oracle

View Code
 1 import java.sql.*;
 2 public class Conn {
 3 
 4     public static void main(String[] args) {
 5 
 6         Statement stem = null;
 7         ResultSet  st = null;
 8         Connection ct = null;
 9         try {
10             Class.forName("oracle.jdbc.driver.OracleDriver");
11             String url="jdbc:oracle:thin:@localhost:1521:ORCL";
12             String UserName="scott";
13             String passWord="m1234";
14             try {
15                 ct=DriverManager.getConnection(url,UserName,passWord);
16                 stem=ct.createStatement();
17                 st=stem.executeQuery("select empno,ename from emp");
18                 while(st.next()){
19                     System.out.println(st.getString("empno")+","+st.getString("ename"));
20                 }
21 
22             } catch (SQLException e) {
23                 
24                 e.printStackTrace();
25             }
26                 
27             
28         } catch (ClassNotFoundException e) {
29             
30             e.printStackTrace();
31         }
32     
33     }
34 }

java连接mysql

View Code
 1         Connection conn = null;
 2         Statement stem = null;
 3         ResultSet rs = null;
 4         try{
 5         String url = "jdbc:mysql://127.0.0.1:3306/users";
 6         String user = "root";
 7         String uPwd = "123456";
 8         try{
 9             //加载驱动
10             Class.forName("com.mysql.jdbc.Driver");
11             //连接数据库
12             conn = DriverManager.getConnection(url,user,uPwd);
13             stem = conn.createStatement();
14             rs = stem.executeQuery("select * from users");
15                 
16                 while(rs.next()){
17                     
18                     System.out.println(rs.getString("uName") + rs.getString("pwd") + rs.getString("id"));
19             
20                 }
21         }catch (Exception e) {
22             e.printStackTrace();
23         }
24         }catch(Exception ex){
25             ex.printStackTrace();
26         }
27     }
28 }

 

转载于:https://www.cnblogs.com/SmileLife/archive/2012/07/25/2607940.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值