public class BaseDao {
private final String DRIVER = "com.mysql.jdbc.Driver";
private final String URL = "jdbc:mysql://localhost:3306/video_db";
private final String USERNAME = "root";
private final String PASSWORD = "123456";
private Connection con = null;
private PreparedStatement pstm = null;
public ResultSet rs = null;
/**
* 获取连接对象
*/
public void getCon(){
try {
Class.forName(DRIVER);
con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 关闭所有连接释放资源
*/
public void closeAll(){
try {
if(null!=rs)
rs.close();
if(null!=pstm)
pstm.close();
if(null!=con)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 查询所有,注意这里不能直接关闭资源
* @param sql
* @param params
* @return
*/
public ResultSet execQuery(String sql,String[] params){
this.getCon();
try {
pstm = con.prepareStatement(sql);
if(params!=null&¶ms.length>0){
for(int i=0;i<params.length;i++){
pstm.setString(i+1, params[i]);
}
}
rs = pstm.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
/**
* 增删改方法
* @return
*/
public int update(String sql,String[] params){
this.getCon();
int result = 0;
try {
pstm = con.prepareStatement(sql);
if(params!=null&¶ms.length>0){
for(int i=0;i<params.length;i++){
pstm.setString(i+1, params[i]);
}
}
result = pstm.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeAll();
}
return result;
}
}
private final String DRIVER = "com.mysql.jdbc.Driver";
private final String URL = "jdbc:mysql://localhost:3306/video_db";
private final String USERNAME = "root";
private final String PASSWORD = "123456";
private Connection con = null;
private PreparedStatement pstm = null;
public ResultSet rs = null;
/**
* 获取连接对象
*/
public void getCon(){
try {
Class.forName(DRIVER);
con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 关闭所有连接释放资源
*/
public void closeAll(){
try {
if(null!=rs)
rs.close();
if(null!=pstm)
pstm.close();
if(null!=con)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 查询所有,注意这里不能直接关闭资源
* @param sql
* @param params
* @return
*/
public ResultSet execQuery(String sql,String[] params){
this.getCon();
try {
pstm = con.prepareStatement(sql);
if(params!=null&¶ms.length>0){
for(int i=0;i<params.length;i++){
pstm.setString(i+1, params[i]);
}
}
rs = pstm.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
/**
* 增删改方法
* @return
*/
public int update(String sql,String[] params){
this.getCon();
int result = 0;
try {
pstm = con.prepareStatement(sql);
if(params!=null&¶ms.length>0){
for(int i=0;i<params.length;i++){
pstm.setString(i+1, params[i]);
}
}
result = pstm.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeAll();
}
return result;
}
}