import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
/**
*
* @author bubei
*
*/
public class DBConn {
String username = "sundun";
String password = "sundun";
String url = "jdbc:oracle:thin:@192.168.1.4:1521:sundun";
String drivername = "oracle.jdbc.driver.OracleDriver";
Connection conn = null;
Statement sta = null;
/**
*
* 構造函數 jdbc數據庫連接
*/
public DbConn() {
}
public Connection getconn() {
try {
try {
Class.forName(drivername).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 執行update,insert,delete語句 參數:sql語句
*/
public int update(String sql) {
int flag = 0;
try {
getconn();
sta = conn.createStatement();
flag = sta.executeUpdate(sql);
System.out.println("執行成功 : 有" + flag + "個記錄被操作");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("發生sql異常後狀態 ! ");
}
return flag;
}
/**
* 執行select語句 參數:sql語句
*/
public ResultSet query(String sql) {
ResultSet rs = null;
try {
getconn();
sta = conn.createStatement();
rs = sta.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("發生sql異常後狀態 ! ");
}
return rs;
}
/**
* 關閉連接
*
*/
public void closeConn() {
try {
sta.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}