你应该是安装mysql的时候编码你是选择默认的吧。
你可以找到mysql的安装目录MySQL Server 5.0\bin\MySQLInstanceConfig.exe
重新配置下就可以了。一般选择utf-8编码。
再一个如果数据库开始就建立好了。alter database 表名 character set utf8;
连接数据库设置编码
jdbc:mysql://地址:3306/数据库名?characterEncoding=utf8
连接数据设置编码
jdbc:mysql://地址:3306/数据库名?autoReconnect=true&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
转义连接
jdbc:mysql://地址:3306/数据库名?autoReconnect=true&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
连接数据的工具类
public final class JdbcUtils {
private static String url = "jdbc:mysql://localhost:3306/jdbc";
private static String user = "root";
private static String password = "";private JdbcUtils() {
}static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
新增数据的方法
public void addUser(User user) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
String sql = "insert into user(name,birthday, money) values (?,?,?) ";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setDate(2, new java.sql.Date(user.getBirthday().getTime()));
ps.setFloat(3, user.getMoney());
ps.executeUpdate();
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
} finally {
JdbcUtils.free(rs, ps, conn);
}
}
删除数据的方法
public void delete(User user) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
st = conn.createStatement();
String sql = "delete from user where id=" + user.getId();
st.executeUpdate(sql);
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
} finally {
JdbcUtils.free(rs, st, conn);
}}
查询数据的方法
public User findUser(String loginName, String password) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
try {
conn = JdbcUtils.getConnection();
String sql = "select id, name, money, birthday from user where name=?";
ps = conn.prepareStatement(sql);
ps.setString(1, loginName);
rs = ps.executeQuery();
while (rs.next()) {
user = mappingUser(rs);
}
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
} finally {
JdbcUtils.free(rs, ps, conn);
}
return user;
}private User mappingUser(ResultSet rs) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setMoney(rs.getFloat("money"));
user.setBirthday(rs.getDate("birthday"));
return user;
}
更新数据的方法
public void update(User user) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
String sql = "update user set name=?, birthday=?, money=? where id=? ";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setDate(2, new java.sql.Date(user.getBirthday().getTime()));
ps.setFloat(3, user.getMoney());
ps.setInt(4, user.getId());
ps.executeUpdate();
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
} finally {
JdbcUtils.free(rs, ps, conn);
}
}
最新评论
主要区别在于 Long 类型可以
感谢分享。。。
您好,您这个是DES加密,不是D
您好,您这个是DES加密,不是D
比别人多一点执着,你就会创造奇迹