|
dao factory
public class DaoFactor {
public Object builddao(String name) {
Object classname=null;
try {
classname= Class.forName(name).newInstance();
} catch (InstantiationException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return classname;
}
}
mysql
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MySqlDao {
Connection con=null;
Statement sta=null;
ResultSet rs=null;
public Connection getcon(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
try {
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ACCP","root","root");
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return con;
}
public void closecon(){
if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
public int update(String sql){
int result=0;
getcon();
try {
sta=this.getcon().createStatement();
result=sta.executeUpdate(sql);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
try {
sta.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
closecon();
}
return result;
}
public ResultSet query(String sql){
con= getcon();
try {
sta=con.createStatement();
rs=sta.executeQuery(sql);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return rs;
}
}
oracle
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class OracleDao {
Connection con=null;
Statement sta=null;
ResultSet rs=null;
public Connection getcon(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
try {
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ACCP","system","manager");
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return con;
}
public void closecon(){
if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
public int update(String sql){
int result=0;
getcon();
try {
sta=con.createStatement();
result=sta.executeUpdate(sql);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
try {
sta.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
closecon();
}
return result;
}
public ResultSet query(String sql){
con = getcon();
try {
sta=con.createStatement();
rs=sta.executeQuery(sql);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return rs;
}
}
sql server
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Sqldao {
Connection con=null;
Statement sta=null;
ResultSet rs=null;
public Connection getcon(){
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
try {
Connection conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ACCP","sa","");
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return con;
}
public void closecon(){
if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
public int update(String sql){
int result=0;
getcon();
try {
sta=con.createStatement();
result=sta.executeUpdate(sql);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
try {
sta.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
closecon();
}
return result;
}
public ResultSet query(String sql){
getcon();
try {
sta=con.createStatement();
rs=sta.executeQuery(sql);
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
try {
rs.close();
sta.close();
con.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
return rs;
}
} |
|
|