2016年3月18日23:02:11 手写给自己,很多时候人只需安静的活着体验存在就是修炼。
public interface DbHelper{
Connection getConnection();
void closeAll();
ResultSet queryMethod(String sql,Object[]param);
Boolean updateOrDelete(String sql,Object[]param);
}
public class DbHelperImpl implements DbHelper{
private static final String URL="jdbc:mysql://192.168.01.01:3456/database?setCharacter=gb2313";
private static final String USERNAME="root";
private static final String PASSWORD="secret";
private Connection conn=null;
private PrepareStament pstmt=null;
private ResultSet rs=null;
public Connection getConnection(){
try{
DriverManager.registerDriver(new com.jdbc.mysql.Driver());
conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
}catch(NotClassFoundException e){
e.stackprint();
}
return conn;
}
}
public void colseAll(){
try{
if(rs!=null){
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if(conn!=null){
conn.close();
}
}catch(IOException e){
e.stackprint();
}
public ResultSet queryMethod(String sql,Object[]param){
getConnection();
pstmt=conn.prepareStament(sql);
if(pstmt!=null){
for(int i=0;i<param.length();i++){
pstmt.setObject(i+1,param[i]);
}
rs=pstmt.excuteQuery();
}
return rs;
}
public Boolean updateOrDelete(String sql,Object[]param){
getConnection();
int i=0;
try{
pstmt=conn.prepareStament(sql);
for(int j=0;j<param.length();j++){
pstmt.setObject(i+1,param[i]);
}
i=pstmt.excuteUpdate();
}catch(NotClassFoundException e){
e.stackprint()
}
if(i>=0){
return true;
}else{
return false;
}
}
}