
String sql ="insert into userinfo(name,pwd) values(?,?)"
PreparedStatement pst=getConnection().prepareStatement(sql)
pst.setString(1,"小明")
pst.setString(2,"123")
pst.executeUpdate()
小伙子给你写了一个通用的增删改查的工具类,你那样写太麻烦
资源文件jdbc.properties
jdbc.Driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/xxx
jdbc.username=root
jdbc.password=root
package com.daoimport 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.List
import java.util.Properties
import org.apache.commons.beanutils.BeanUtils
import org.apache.log4j.Logger
public class BaseDao {
private static String DRIVDR
private static String URL
private static String USER
private static String PWD
private static Connection connection
private static Properties properties = new Properties()
private static Logger logger = Logger.getLogger(BaseDao.class)
private static PreparedStatement pst
private static ResultSet rs
private static final String CLASS_NOT_EXCEPTION = "驱动加载失败"
static {
try {
properties.load(BaseDao.class.getResourceAsStream("/jdbc.properties"))
DRIVDR = properties.getProperty("jdbc.Driver")
URL = properties.getProperty("jdbc.url")
USER = properties.getProperty("jdbc.username")
PWD = properties.getProperty("jdbc.password")
Class.forName(DRIVDR)
} catch (Exception e) {
logger.debug(CLASS_NOT_EXCEPTION + e.getMessage())
}
}
/**
* 通用的增删改
*
* @param sql
* @param args
* @return
*/
public static int executeCommand(String sql, Object... args) {
int m = 0
try {
initPreparedStatement(sql, args)
m = pst.executeUpdate()
} catch (Exception e) {
logger.debug("执行增、删、该。错误。。请检查preparedStatement参数。。。"+e.getMessage())
} finally {
closeAll(null, pst, connection)
}
return m
}
private static PreparedStatement initPreparedStatement(String sql,Object...args){
try {
pst=getConnection().prepareStatement(sql)
if(args!=null){
for(int i=0i<args.lengthi++){
pst.setObject(i+1, args[i])
}
}
} catch (Exception e) {
logger.debug("为pst对象赋值错误。。请检查preparedStatement参数。。。"+e.getMessage())
}
return pst
}
/**
* 通用的执行聚合函数
* @param sql
* @param args
* @return
*/
public static int executeScalare(String sql,Object...args){
int count=0
initPreparedStatement(sql, args)
try {
rs=pst.executeQuery()
if(rs.next()){
count=rs.getInt(1)
}
} catch (SQLException e) {
logger.debug("执行聚合函数出错。。。请检查preparedStatement参数。。。"+e.getMessage())
}finally{
closeAll(rs, pst, connection)
}
return count
}
/**
* 根据id查询单个对象
* @param sql
* @param clazz
* @param args
* @return
*/
public static <T> T findById(String sql,Class<T> clazz,Object...args){
T t = null
try {
initPreparedStatement(sql, args)
rs = pst.executeQuery()
ResultSetMetaData metaData = rs.getMetaData()
int count = metaData.getColumnCount()
// 获取字段的数量
if(rs.next()) {
try {
t = clazz.newInstance()// 利用反射自动创建对象的类型的对象 User.class User
// u=new User()
for (int i = 1 i <= count i++) {
BeanUtils.copyProperty(t, metaData.getColumnName(i), rs.getObject(i))
// 自动获取各个字段的名称并获取该字段的值
}
} catch (Exception e) {
logger.debug("查询单个对象,错误。。请检查preparedStatement参数。。。。"+e.getMessage())
}
}
} catch (SQLException e) {
logger.debug("查询失败。。。。。" + e.getMessage())
}
return t
}
/**
* 通用的查询
*
* @param sql
* @param clazz
* @param args
* @return
*/
public static <T> List<T> findAll(String sql, Class<T> clazz, Object... args) {
List<T> list = new ArrayList<T>(100)
T t = null
try {
initPreparedStatement(sql, args)
rs = pst.executeQuery()
ResultSetMetaData metaData = rs.getMetaData()
// 以上的代码:获取元数据(各个字段的数据类型)
int count = metaData.getColumnCount()
// 获取字段的数量
while (rs.next()) {
try {
t = clazz.newInstance()// 利用反射自动创建对象的类型的对象 User.class User
// u=new User()
for (int i = 1 i <= count i++) {
BeanUtils.copyProperty(t, metaData.getColumnName(i), rs.getObject(i))
// 自动获取各个字段的名称并获取该字段的值
}
} catch (Exception e) {
logger.debug("查询集合,错误。。。。。"+e.getMessage())
}
list.add(t)// 将对象添加到集合中
}
} catch (SQLException e) {
logger.debug("查询失败。。。。。" + e.getMessage())
}
return list
}
public static Connection getConnection() {
try {
if (connection == null || connection.isClosed()) {
connection = DriverManager.getConnection(URL, USER, PWD)
}
} catch (SQLException e) {
logger.debug("获取connection失败,请检查配置文件!" + e.getMessage())
}
return connection
}
public static void closeAll(ResultSet rs, PreparedStatement pst, Connection conn) {
if (rs != null)
try {
rs.close()
} catch (SQLException e) {
logger.debug("关闭ResultSet错误。。。。。" + e.getMessage())
}
if (pst != null)
try {
pst.close()
} catch (SQLException e) {
logger.debug("关闭PreparedStatement错误。。。。。" + e.getMessage())
}
if (conn != null)
closeConnection(conn)
}
private static void closeConnection(Connection conn) {
try {
if (!conn.isClosed()) {
conn.close()
}
} catch (SQLException e) {
logger.debug("关闭Connection错误。。。。。" + e.getMessage())
} finally {
conn = null
}
}
public static void main(String[] args) {
System.out.println(BaseDao.getConnection())
}
}
你应该是安装mysql的时候编码你是选择默认的吧。你可以找到mysql的安装目录MySQL Server 5.0\bin\MySQLInstanceConfig.exe
重新配置下就可以了。一般选择utf-8编码。
再一个如果数据库开始就建立好了。alter database 表名 character set utf8
连接数据库设置编码
jdbc:mysql://地址:3306/数据库名?characterEncoding=utf8
如果是windows的话
1、中止MySQL服务
2、在MySQL的安装目录下找到my.ini,如果没有就把my-medium.ini复制为一个my.ini即可
3、打开my.ini以后,在[client]和[mysqld]下面均加上default-character-set=utf8,保存并关闭
4、启动MySQL服务
你连接数据库的时候,带上编码格式如: jdbc:mysql://localhost/test?useunicode=true&characterencoding=utf8
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)