package util;
import java.sql.*;
public class ConnectionDB {
private static Connection conn = null;
private static String driver = null;
private static String url = null;
private static String user = null;
private static String pass = null;
private ConnectionDB() {
}
public static Connection getOracleCon() {
driver = "oracle.jdbc.driver.OracleDriver";
url = "jdbc:oracle:thin:@localhost:1521:itstar";// itstarΪ��ݿ����
user = "itstar";
pass = "itstar";
try {
Class.forName(driver);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
if (conn == null) {
conn = DriverManager.getConnection(url, user, pass);
}
}
catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Connection getSqlCon() {
driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dbName";// dbNameΪ��ݿ����
user = "sa";
pass = "sa";
try {
Class.forName(driver);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
if (conn == null) {
conn = DriverManager.getConnection(url, user, pass);
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
Connection connection = ConnectionDB.getOracleCon();
try {
Statement state = connection.createStatement();
ResultSet rs = state.executeQuery("select * from users");
//
ResultSet rs = state
//
.executeQuery("insert into usersvalues('liu','123456')");
while (rs.next()) {
System.err.println(rs.getString(1));
System.err.println(rs.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}