09..操作数据库
2016-06-17 11:08
295 查看
代码:
public class TestMysql {
public static voidmain(String[] args) {
// 1、加载JDBC驱动程序
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
System.err.println("找不到驱动程序类
,加载驱动失败!");
}
// 2、创建数据库的连接
String url ="jdbc:mysql://localhost:3306/cdbp?useUnicode=true&characterEncoding=gbk";
String username ="root";
String password ="";
Connection conn =null;
try {
conn =DriverManager.getConnection(url, username, password);
// 3、创建一个Statement
String sql = "select count(id)from t_patient";
PreparedStatement stmt =conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
// 4、处理数据
while (rs.next()) {
System.out.println(rs.getString(1));
}
// 5、关闭连接对象
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch(SQLException se) {
System.err.println("数据库操作败!");
} finally {
try {
conn.close();
} catch
a6e2
(SQLException e) {
e.printStackTrace();
}
}
}
}
public class TestMysql {
public static voidmain(String[] args) {
// 1、加载JDBC驱动程序
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
System.err.println("找不到驱动程序类
,加载驱动失败!");
}
// 2、创建数据库的连接
String url ="jdbc:mysql://localhost:3306/cdbp?useUnicode=true&characterEncoding=gbk";
String username ="root";
String password ="";
Connection conn =null;
try {
conn =DriverManager.getConnection(url, username, password);
// 3、创建一个Statement
String sql = "select count(id)from t_patient";
PreparedStatement stmt =conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
// 4、处理数据
while (rs.next()) {
System.out.println(rs.getString(1));
}
// 5、关闭连接对象
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch(SQLException se) {
System.err.println("数据库操作败!");
} finally {
try {
conn.close();
} catch
a6e2
(SQLException e) {
e.printStackTrace();
}
}
}
}
相关文章推荐
- 微软SQLHelper.cs类 中文版
- 数据库单表的查询
- 【mysql】SQL常用指令
- Oracle如何根据一个日期计算同比环比的日期
- redis命令参考
- mongodb 常用命令
- 分布式数据库需要考虑的(BigTable VS Dynamo)
- ELMAH日志组件数据库脚本
- Oracle VM VirtualBox 下 ubuntu 虚拟机 如何 挂载共享
- 故障案例--mysql5.5 myisam引擎出现Waiting for table metadata lock
- SQLSERVER分页
- iBatis 控制台输出 sql 语句
- windows2003服务器mysql每天定时备份
- 外部数据库的引入
- 常用的sql 查询
- Mysql中随机函数笔记
- mycat实现mysql读写分离,热切换,集群
- PostgreSQL 9.2 创建数据库,导入和导出数据库,创建超级用户
- 论文实验遇到的问题(hadoop集群+redis集群)
- 实战录 | 基于redis的分布式HA调度器解决方案