java數據庫編程

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class DBConnection {

private String drv = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost/database?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false";

private String usr = "hkeaweb";

private String pwd = "hkeawebpass";

private Connection conn = null;

private Statement stm = null;

private PreparedStatement ps = null;

private ResultSet rs = null;

public PreparedStatement getPs() {
return ps;
}

public void setPs(PreparedStatement ps) {
this.ps = ps;
}

public boolean createConn() {
boolean b = false;
try {
Class.forName(drv).newInstance();
conn = DriverManager.getConnection(url, usr, pwd);
b = true;
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return b;
}
//PreparedStatement execute  return boolean
public boolean execute(String sql,List param) {
boolean b = false;
try {
ps = conn.prepareStatement(sql);
if(param!=null){
for(int i=0;i<param.size();i++){
ps.setObject(i+1, param.get(i));
}
}
ps.execute();
} catch (Exception e) {
e.printStackTrace();
}
return b;
}
//Statement execute  return boolean
public boolean execute(String sql) {
boolean b = false;
try {
stm = conn.createStatement();
b=stm.execute(sql);
} catch (Exception e) {
e.printStackTrace();
}
return b;
}
//Statement executeUpdate  return int
public int executeUpdate(String sql) {
int result = 0;
try {
stm = conn.createStatement();
result=stm.executeUpdate(sql);

} catch (Exception e) {e.printStackTrace();
}
return result;
}
//PreparedStatement executeUpdate  return int
public int executeUpdate(String sql,List param) {
int result = 0;
try {
ps = conn.prepareStatement(sql);
if(param!=null){
for(int i=0;i<param.size();i++){
ps.setObject(i+1, param.get(i));
}
}
System.out.println(ps);
result=ps.executeUpdate();
} catch (Exception e) {e.printStackTrace();
}
return result;
}
//Statement executeQuery  return ResultSet
public ResultSet executeQuery(String sql) {
try {
stm = conn.createStatement();
rs = stm.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
//PreparedStatement executeQuery  return ResultSet
public ResultSet executeQuery(String sql,List param) {
try {
ps = conn.prepareStatement(sql);
if(param!=null){
for(int i=0;i<param.size();i++){
ps.setObject(i+1, param.get(i));
}
}
rs=ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
public String getValue(String field) {
String value = null;
try {
if (rs != null)
value = rs.getString(field);
} catch (Exception e) {
}
return value;
}

public void closeConn() {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
}
}
    public void closePt(){
    try{
    if(ps!=null){
    ps.close();
    }
    } catch (SQLException e) {
}
    }
public void closeStm() {
try {
if (stm != null)
stm.close();
} catch (SQLException e) {
}
}

public void closeRs() {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
}
}

public Connection getConn() {
return conn;
}

public void setConn(Connection conn) {
this.conn = conn;
}

public String getDrv() {
return drv;
}

public void setDrv(String drv) {
this.drv = drv;
}

public String getPwd() {
return pwd;
}

public void setPwd(String pwd) {
this.pwd = pwd;
}

public ResultSet getRs() {
return rs;
}

public void setRs(ResultSet rs) {
this.rs = rs;
}

public Statement getStm() {
return stm;
}

public void setStm(Statement stm) {
this.stm = stm;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getUsr() {
return usr;
}

public void setUsr(String usr) {
this.usr = usr;
}

}

你可能感兴趣的:(java,mysql)