java连接数据库的close方法_Java技术Jdbc连接数据库方法及使用方法

你应该是安装mysql的时候编码你是选择默认的吧。

a6fa469acd510b052ae71cc23b18495f.gif

你可以找到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);

}

}

*文章为作者独立观点,不代表上流阁立场

本文由 江风成 授权 上流阁 发表,并经上流阁编辑。转载此文章须经作者同意,并请附上出处(上流阁)及本页链接。原文链接https://www.o6c.com/java/2017/03/21/961.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值