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(); } } } }
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(); } } } }
时间: 2024-10-02 10:38:45