JDBC的典型用法
来源:互联网 发布:免费的代理服务器软件 编辑:程序博客网 时间:2024/06/12 10:02
JDBC驱动程序是Java程序和数据库之间的转换层,数据库驱动程序负责将JDBC调用成特定的数据库。
JDBC操作数据库的步骤:
代码示例
publicstatic void main(String[]args) {
Connection conn =null;
PreparedStatement ps =null;
try {
// 1.加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 2.获取数据库连接
// 其中url叫套接字,不同的数据库不同的定义规范
// MySQL: jdbc:mysql://hostname:port/databasename
// Oracle: jdbc:oracle:thin:@hostname:port:databasename
// 作为本机:localhost 127.0.0.1
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tech2","root", "");
// 3.创建查询对象
ps = conn.prepareStatement("select id,name,code,age,sex from t_student");
// 4.获取查询的结果集
ResultSet rs =ps.executeQuery();
// 5.遍历结果集输出数据
while (rs.next()) {
String name =rs.getString("name");
intage = rs.getInt("age");
System.out.println("name:" +name + "\tage:" +age);
}
} catch (ClassNotFoundExceptione) {
System.out.println("驱动加载失败");
} catch (SQLExceptione) {
System.out.println("查询对象创建失败");
} catch (Exceptione) {
e.printStackTrace();
} finally {
// 6.关闭连接
if (null !=ps) {
try {
ps.close();
} catch (SQLExceptione) {
e.printStackTrace();
}
}
if (null !=conn) {
try {
conn.close();
} catch (SQLExceptione) {
e.printStackTrace();
}
}
}
}
- JDBC的典型用法
- JDBC的典型用法
- JDBC的典型用法
- JAVA学习随笔之JDBC的典型用法之DriverManager
- StringTokenizer类的典型用法
- composite-element的典型用法
- Filter函数的典型用法
- Match函数的典型用法
- golang的select典型用法
- jdbc连接各种数据库的典型代码
- 一个典型的JDBC小程序
- JDBC访问数据库的典型操作步骤
- wget的几种典型用法(介绍典型的)
- Win32关键区的一种典型用法
- boost库regex的典型用法 [转载]
- [转载]快递接口的典型用法
- C#字符串类的典型用法
- BCB TOpenDialog组件的典型用法
- 如何遍历链表
- 对于创业企业,盈利和用户谁应该放在第一位
- Android自定义视图(一)——onMeasure,MeasureSpec源码 流程 思路详解
- Android Glide数据更新及内存缓存、硬盘缓存清理
- TypeScript类型系统和基础类型
- JDBC的典型用法
- textarea 存入数据库后 换行 、空格显示
- 单例模式
- [数据预处理]one-hot编码
- wget在进行https下载时超时不生效问题
- SpringMVC原理与SpringMVC常用注解
- 把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。
- 20170705
- cocos2d-x-lua 单点触摸事件