jdbc连接数据库
2016-05-27 20:21
423 查看
package testdb;
import java.sql.*;
public class TestDb {
public static void main(String[] args) {
String strDBDriver="com.mysql.jdbc.Driver";
String strDBUrl="jdbc:mysql://localhost:3306/test";
try {
Class.forName(strDBDriver).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection conn = null;
try {
conn = DriverManager.getConnection(strDBUrl,"root","root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//创建数据库连接对象
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String sql="select count(*) as num from i_s_logon";
try {
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println("num="+rs.getInt("num"));
//System.out.println("password="+rs.getString("password"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.sql.*;
public class TestDb {
public static void main(String[] args) {
String strDBDriver="com.mysql.jdbc.Driver";
String strDBUrl="jdbc:mysql://localhost:3306/test";
try {
Class.forName(strDBDriver).newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection conn = null;
try {
conn = DriverManager.getConnection(strDBUrl,"root","root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//创建数据库连接对象
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String sql="select count(*) as num from i_s_logon";
try {
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println("num="+rs.getInt("num"));
//System.out.println("password="+rs.getString("password"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相关文章推荐
- jdbc中的Statement和PreparedStatement接口对象
- Android之获取手机上的图片和视频缩略图thumbnails
- 数据库链接字符串查询网站
- DB2实例管理
- DB2实例管理
- 保障MySQL数据安全的14个最佳方法
- mysql问答汇集
- 第三章 数据库备份和还原
- 创建一个空的IBM DB2 ECO数据库的方法
- Access 2000 数据库 80 万记录通用快速分页类
- 开通一个数据库失败的原因的和解决办法
- 一个简单的asp数据库操作类
- CentOS下DB2数据库安装过程详解
- EasyASP v1.5发布(包含数据库操作类,原clsDbCtrl.asp)第1/2页
- sql2008 还原数据库解决方案
- Oracle 数据库自动存储管理-安装配置
- Oracle数据库执行脚本常用命令小结
- Oracle 数据库 临时数据的处理方法
- 数据库分页查询语句数据库查询