package JDBC_1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class jdbcUtil {
String driver = "com.mysql.jdbc.Driver";
String url ="jdbc:mysql://127.0.0.1:3306/test";
String username = "root";
String password = "123";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
public Connection getConn() {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,username,password);
}catch (Exception e){
e.printStackTrace();
}
return conn;
}
public int excuteUP(String sql, Object []pp) {
try {
ps = getConn().prepareStatement(sql);
if(pp != null){
for (int i = 0; i<pp.length;i++){
ps.setObject(i+1,pp[i]);
}
}
int result = ps.executeUpdate();
return result;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public ResultSet excuteSel(String sql,Object[] pp){
try {
ps = getConn().prepareStatement(sql);
if (pp != null){
for (int i = 0; i < pp.length; i++){
ps.setObject(i+1,pp[i]);
}
}
rs = ps.executeQuery();
this.closeAll(conn,ps,null);
}catch (Exception e){
e.printStackTrace();
}
return rs;
}
public void closeAll(Connection conn,PreparedStatement ps,ResultSet rs){
try {
if (rs != null){
conn.close();
}
if (ps != null){
rs.close();
}
if (conn != null){
conn.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}