一、jdbc连接Oracle数据库
package com.nancal.checking_in_remind.util;
import java.sql.*;
public class DB_Oracle {
public static Connection GetConnection(){
Connection conn = null;
Statement st;
ResultSet rs ;
String URl = "jdbc:oracle:thin:@10.214.7.141:1521:orcl";
String driver = "oracle.jdbc.driver.OracleDriver";
String user = "hbshflow";
String password = "1";
try {
Class.forName(driver); //装载驱动
conn = DriverManager.getConnection(URl,user,password); //获得连接
st = conn.createStatement(); //声明
String sql = "SELECT * FROM SYS_PERSON n WHERE n.CNNAME = (SELECT t.name FROM NANCAL_ADDRESSBOOK t WHERE t.name='李四')";
rs = st.executeQuery(sql); //结果集
while(rs.next()){
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void main(String args[]){
GetConnection();
}
}
二、JDBC连接SQLServer数据库
package com.nancal.checking_in_remind.util;
import java.sql.*;
public class DB_SQLServer {
//测试连接SQL数据库
public static Connection GetConnection(){
Connection conn = null;
Statement st;
ResultSet rs ;
String URl = "jdbc:sqlserver://10.214.8.252:1433;databaseName= XXX";
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String user = "sa";
String password = "123";
try {
Class.forName(driver); //装载驱动
conn = DriverManager.getConnection(URl,user,password); //获得连接
st = conn.createStatement(); //声明
String sql = "select * from dbo.GM_KQ_TimeRecorderData t where employeeid='990049' order by builddatetime desc"; //SQL语句
rs = st.executeQuery(sql); //结果集
while(rs.next()){
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void main(String args[]){
GetConnection();
}