在没有配置环境的条件下JdbcUtil.java
public class JDBCUtil {
//表示定义数据库的用户名
private final String USERNAME="root";
//定义数据库的密码
private final String PASSWORD="root";
//定义数据库的驱动信息
private final String DRIVER="com.mysql.jdbc.Driver";
//定义访问数据库到的地址
private final String URL="jdbc:mysql://localhost:3306/studentpage";
//定义数据库的连接
private Connection conn;
//定义sqk语句的执行对象
private PreparedStatement pstmt;
//定义查询返回的结果集
private ResultSet resultSet;
public JDBCUtil(){
}
/**
* 获取数据库连接对象
*/
public Connection getConnection(){
try{
Class.forName(DRIVER);//注册驱动
conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
}catch(Exception e){
throw new RuntimeException("get connection error!");
}
return conn;
}
//执行更新操作
public boolean updateByPreparedStatement(String sql,List<?> params)
throws SQLException{
boolean flag=false;
int result=-1;//表示当前用户执行添加删除和修改的时候所影响的数据行数
pstmt =conn.prepareStatement(sql);
int index=1;
//填充sql语句中的占位符
if(params!=null&&!params.isEmpty()){
for(int i=0;i<params.size();i++){
pstmt.setObject(index++,params.get(i));
}
}
result=pstmt.executeUpdate();
flag=result>0?true:false;
return flag;
}
//执行查询操作
public List<Map<String,Object>>findResult(String sql,List<?> params)
throws SQLException{
List<Map<String,Object>>list =new ArrayList<Map<String,Object>>();
int index=1;
pstmt=conn.prepareStatement(sql);
if(params!=null&&!params.isEmpty()){
for(int i=0;i<params.size();i++){
pstmt.setObject(index++,params.get(i));
}
}
resultSet=pstmt.executeQuery();
ResultSetMetaData metaData=resultSet.getMetaData();
int cols_len=metaData.getColumnCount();
while(resultSet.next()){
Map<String,Object>map=new HashMap<String,Object>();
for(int i=0;i<cols_len;i++){
String cols_name=metaData.getColumnName(i+1);
Object cols_value=resultSet.getObject(cols_name);
if(cols_value==null){
cols_value="";
}
map.put(cols_name, cols_value);
}
list.add(map);
}
return list;
}
/**
* 释放资源
*/
public void releaseConn(){
if(resultSet!=null){
try{
resultSet.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(pstmt!=null){
try{
pstmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
在配置文件下的JdbcUtils.java
配置文件:
jdbc.username=root
jdbc.password=root
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/studentpage
JdbcUtils.java
public class JDBCUtil {
//表示定义数据库的用户名
private static String USERNAME;
//定义数据库的密码
private static String PASSWORD;
//定义数据库的驱动信息
private static String DRIVER;
//定义访问数据库到的地址
private static String URL;
//定义数据库的连接
private Connection conn;
//定义sqk语句的执行对象
private PreparedStatement pstmt;
//定义查询返回的结果集
private ResultSet resultSet;
static{
loadConfig();
}
/**
* 加载数据库配置信息,并给相关的属性赋值
*/
public static void loadConfig(){
try {
InputStream inStream=JDBCUtil.class.getResourceAsStream("/jdbc.properties");
Properties prop=new Properties();
prop.load(inStream);
USERNAME=prop.getProperty("jdbc.username");
PASSWORD=prop.getProperty("jdbc.password");
DRIVER=prop.getProperty("jdbc.driver");
URL=prop.getProperty("jdbc.url");
} catch (Exception e) {
throw new RuntimeException("读取数据库配置文件异常!",e);
}
}
public JDBCUtil(){
}
/**
* 获取数据库连接对象
*/
public Connection getConnection(){
try{
Class.forName(DRIVER);//注册驱动
conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
}catch(Exception e){
throw new RuntimeException("get connection error!");
}
return conn;
}
//执行更新操作
public boolean updateByPreparedStatement(String sql,List<?> params)
throws SQLException{
boolean flag=false;
int result=-1;//表示当前用户执行添加删除和修改的时候所影响的数据行数
pstmt =conn.prepareStatement(sql);
int index=1;
//填充sql语句中的占位符
if(params!=null&&!params.isEmpty()){
for(int i=0;i<params.size();i++){
pstmt.setObject(index++,params.get(i));
}
}
result=pstmt.executeUpdate();
flag=result>0?true:false;
return flag;
}
//执行查询操作
public List<Map<String,Object>>findResult(String sql,List<?> params)
throws SQLException{
List<Map<String,Object>>list =new ArrayList<Map<String,Object>>();
int index=1;
pstmt=conn.prepareStatement(sql);
if(params!=null&&!params.isEmpty()){
for(int i=0;i<params.size();i++){
pstmt.setObject(index++,params.get(i));
}
}
resultSet=pstmt.executeQuery();
ResultSetMetaData metaData=resultSet.getMetaData();
int cols_len=metaData.getColumnCount();
while(resultSet.next()){
Map<String,Object>map=new HashMap<String,Object>();
for(int i=0;i<cols_len;i++){
String cols_name=metaData.getColumnName(i+1);
Object cols_value=resultSet.getObject(cols_name);
if(cols_value==null){
cols_value="";
}
map.put(cols_name, cols_value);
}
list.add(map);
}
return list;
}
/**
* 释放资源
*/
public void releaseConn(){
if(resultSet!=null){
try{
resultSet.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(pstmt!=null){
try{
pstmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}