码迷,mamicode.com
首页 > 数据库 > 详细

JDBC

时间:2018-01-16 14:07:00      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:null   exe   dstat   get   code   object   list   state   sql   

package com.com.Dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class BaseDao {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/xiangmuyi?user=root&password=root&useUnicode=true&characterEncoding=UTF8";
PreparedStatement state=null;
Connection conn=null;
ResultSet result=null;

public void cleaveDriver(){
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage()+"驱动异常");
}
}
public void createObject(String sql){
cleaveDriver();
try {
conn=DriverManager.getConnection(url);
state=conn.prepareStatement(sql);
} catch (SQLException e) {
System.out.println(e.getMessage()+"连接异常");
}

}
public void closeObject(){
try {
if(state!=null)
state.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
System.out.println(e.getMessage()+"关闭异常");
}
}
public ResultSet Selectxiangmuyi(String sql,ArrayList<Object> array){
createObject(sql);
try {
if(array!=null){
for(int i=0;i<array.size();i++){
state.setObject(i+1, array.get(i));
}
}
ResultSet result=state.executeQuery();
return result;
} catch (SQLException e) {
System.out.println(e.getMessage()+"查询异常");
return null;
}

}
public boolean Updatexiangmuyi(String sql,ArrayList<Object> array){
createObject(sql);
try {
if(array!=null){
for(int i=0;i<array.size();i++){
state.setObject(i+1, array.get(i));
}
}
int result=state.executeUpdate();
return result>0;
} catch (SQLException e) {
System.out.println(e.getMessage()+"增删改异常");
return false;
}finally{
closeObject();
}
}


}

JDBC

标签:null   exe   dstat   get   code   object   list   state   sql   

原文地址:https://www.cnblogs.com/yuan211/p/8295752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!