package ddc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DataFactory {
public static Connection getCon() {
Connection con = null;
PreparedStatement pre = null;
ResultSet result = null;
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String user = "scott";
String pass = "tiger";
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, pass);
String sql = "select * from emp";
pre = con.prepareStatement(sql);
result = pre.executeQuery();
while (result.next()) {
System.out.print(result.getString("empno")+" ");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(con != null){
con.close();
}
if(pre != null){
pre.close();
}
if(result != null){
result.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return con;
}
public static void main(String[] args) {
getCon();
}
}
相关文章: