日常开发中,总是需要操作数据库,然而有些不能通过sql实现的,我们可以通过java来实现,比如批量插入测试数据,循环匹配指定数据等等
- 准备jar
- mysql-connector-java-5.1.10.jar
- javautil工具类
package com.test.util;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* <p class="detail">
* 功能:java链接数据库util
* </p>
* @ClassName: JdbcUtils
* @version V1.0
* @date 2016-12-5
* @author damowang
*/
public class JdbcUtils {
//数据库用户名
private static final String USERNAME = "root";
//数据库密码
private static final String PASSWORD = "123456";
//驱动信息
private static final String DRIVER = "com.mysql.jdbc.Driver";
//数据库地址
private static final String URL = "jdbc:mysql://127.0.0.1/pettyloan";
private Connection connection;
private PreparedStatement pstmt;
private ResultSet resultSet;
public JdbcUtils() {
// TODO Auto-generated constructor stub
try{
Class.forName(DRIVER);
System.out.println("数据库连接成功!");
}catch(Exception e){
}
}
/**
* 获得数据库的连接
* @return
*/
public Connection getConnection(){
try {
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
/**
* 增加、删除、改
* @param sql
* @param params
* @return
* @throws SQLException
*/
public boolean updateByPreparedStatement(String sql, List<Object>params)throws SQLException{
boolean flag = false;
int result = -1;
pstmt = connection.prepareStatement(sql);
int index = 1;
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;
}
/**
* 查询单条记录
* @param sql
* @param params
* @return
* @throws SQLException
*/
public Map<String, Object> findSimpleResult(String sql, List<Object> params) throws SQLException{
Map<String, Object> map = new HashMap<String, Object>();
int index = 1;
pstmt = connection.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 col_len = metaData.getColumnCount();
while(resultSet.next()){
for(int i=0; i<col_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);
}
}
return map;
}
/**查询多条记录
* @param sql
* @param params
* @return
* @throws SQLException
*/
public List<Map<String, Object>> findModeResult(String sql, List<Object> params) throws SQLException{
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
int index = 1;
pstmt = connection.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;
}
/**通过反射机制查询单条记录
* @param sql
* @param params
* @param cls
* @return
* @throws Exception
*/
public <T> T findSimpleRefResult(String sql, List<Object> params,
Class<T> cls )throws Exception{
T resultObject = null;
int index = 1;
pstmt = connection.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()){
//通过反射机制创建一个实例
resultObject = cls.newInstance();
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 = "";
}
Field field = cls.getDeclaredField(cols_name);
field.setAccessible(true); //打开javabean的访问权限
field.set(resultObject, cols_value);
}
}
return resultObject;
}
/**通过反射机制查询多条记录
* @param sql
* @param params
* @param cls
* @return
* @throws Exception
*/
public <T> List<T> findMoreRefResult(String sql, List<Object> params,
Class<T> cls )throws Exception {
List<T> list = new ArrayList<T>();
int index = 1;
pstmt = connection.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()){
//通过反射机制创建一个实例
T resultObject = cls.newInstance();
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 = "";
}
Field field = cls.getDeclaredField(cols_name);
field.setAccessible(true); //打开javabean的访问权限
field.set(resultObject, cols_value);
}
list.add(resultObject);
}
return list;
}
/**
* 释放数据库连接
*/
public void releaseConn(){
if(resultSet != null){
try{
resultSet.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
JdbcUtils jdbcUtils = new JdbcUtils();
jdbcUtils.getConnection();
/*******************增*********************/
/* String sql = "insert into userinfo (username, pswd) values (?, ?), (?, ?), (?, ?)";
List<Object> params = new ArrayList<Object>();
params.add("damowang");
params.add("123456");
params.add("大魔王");
params.add("123456");
try {
boolean flag = jdbcUtils.updateByPreparedStatement(sql, params);
System.out.println(flag);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
/*******************删*********************/
/* String sql = "delete from userinfo where username = ?";
List<Object> params = new ArrayList<Object>();
params.add("大魔王");
boolean flag = jdbcUtils.updateByPreparedStatement(sql, params);*/
/*******************改*********************/
/* String sql = "update userinfo set pswd = ? where username = ? ";
List<Object> params = new ArrayList<Object>();
params.add("123456");
params.add("damowang");
boolean flag = jdbcUtils.updateByPreparedStatement(sql, params);
System.out.println(flag);*/
/*******************查*********************/
/* String sql2 = "select * from userinfo ";
List<Map<String, Object>> list = jdbcUtils.findModeResult(sql2, null);
System.out.println(list);*/
//利用反射查询 单条记录
String sql = "select * from soft_page_theme ";
List<Object> params = new ArrayList<Object>();
List list =jdbcUtils.findModeResult(sql, params);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).toString());
}
}
}
查询结果