在eclipse中Java连接Sybase和oracle的例子


1.连接Sybase

1)准备工作:

准备Sybase驱动jconn2.jar,在eclipse中引用这个jar包:

在项目上右键>Build Path>Add External Archives...,找到jconn2.jar,点确定。

  note:网上有一片文章,把jconn2.jar放到CLASSPATH环境变量中,这种方法在命令行中用javac手工编译可行。在Eclipse中需要把jconn2.jar引入项目才行。

2)一段示例代码

package connDB;

import java.sql.*;
import java.util.Properties;

public class DataSwift {
    public static void main(String [] args){
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;
        try{
            Class.forName("com.sybase.jdbc2.jdbc.SybDriver");
            String url = "jdbc:sybase:Tds:localhost:5000/cwbase15";
            Properties props = System.getProperties();
            props.put("user", "sa");
            props.put("password", "sasasa");
            conn = DriverManager.getConnection(url,props);
            statement = conn.createStatement();
            rs = statement.executeQuery("SELECT * FROM TEST");
            while(rs.next()){
                System.out.println(rs.getString("TEST"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(rs!=null) rs.close();
                if(statement!=null) statement.close();
                if(conn!=null) conn.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}
View Code

2.连接Oracle

1)准备工作

准备Oracle驱动ojdbc14.jar,在eclipse中引用这个jar包。方法同上。

2)一段示例代码:

package connDB;

import java.sql.*;
import java.util.Properties;

public class ConnOracle {
    public static void main(String [] args){
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;
        try{
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@localhost:1521:orcl";
            Properties props = System.getProperties();
            props.put("user", "system");
            props.put("password", "abcd1234");
            conn = DriverManager.getConnection(url,props);
            statement = conn.createStatement();
            rs = statement.executeQuery("SELECT * FROM TEST");
            while(rs.next()){
                System.out.println(rs.getString("NAME"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(rs!=null) rs.close();
                if(statement!=null) statement.close();
                if(conn!=null) conn.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}
View Code

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM