JDBC
public class JDBC {
public static void main(String[] args) throws Exception {
Connection connection = null;
PreparedStatement prepareStatement = null;
ResultSet rs = null;
try {
// 加载驱动
Class.forName("com.MysqL.jdbc.Driver");
// 获取连接
String url = "jdbc:MysqL://127.0.0.1:3306/ssmdemo";
String user = "root";
String password = "123456";
connection = DriverManager.getConnection(url, user, password);
// 获取statement,preparedStatement
String sql = "select * from tb_user where id=?";
prepareStatement = connection.prepareStatement(sql);
// 设置参数
prepareStatement.setLong(1, 1l);
// 执行查询
rs = prepareStatement.executeQuery();
// 处理结果集
while (rs.next()) {
System.out.println(rs.getString("userName"));
System.out.println(rs.getString("name"));
System.out.println(rs.getInt("age"));
System.out.println(rs.getDate("birthday"));
}
} finally {
// 关闭连接,释放资源
if (rs != null) {
rs.close();
}
if (prepareStatement != null) {
prepareStatement.close();
}
if (connection != null) {
connection.close();
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。