package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DbOperat {
Connection conn=null;
PreparedStatement prst= null;
ResultSet rs=null;
public Connection getconn(String server,String dbname,String user,String pwd)throws Exception
{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://"+server+":3306/"+dbname+"?user="+user+"&password="+pwd+"&useUnicode=true&characterEncoding=utf8";
Class.forName(driver).newInstance();
conn=DriverManager.getConnection(url);
return conn;
}
public void closeAll() {
if(rs!=null){
try{
rs.close();
}catch (Exception e) {
e.printStackTrace();
}
}
if(prst!=null){
try{
prst.close();
}catch (Exception e) {
e.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
public ResultSet executeQuery(String prepareSql,String[] param){
try{
prst=conn.prepareStatement(prepareSql);
if(param!=null){
for(int i=0;i<param.length;i++){
prst.setString(i+1, param[i]);
}
}
rs=prst.executeQuery();
}catch (Exception e) {
e.printStackTrace();
}
return rs;
}
public int executeUpdate(String prepareSql,String[] param){
int num=0;
try{
prst=conn.prepareStatement(prepareSql);
if(param!=null){
for(int i=0;i<param.length;i++){
prst.setString(i+1, param[i]);
}
}
num=prst.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}
return num;
}
}