Java Web系列之JDBC,
分享于 点击 30656 次 点评:135
Java Web系列之JDBC,
目录
- JDBC
- JDBC API主要功能
- JDBC访问数据库的具体步骤
- JDBCDemo代码
- 数据库驱动
- 处理CLOB/BLOB类型
- 总结
本文最初由security发布于security的博客,禁止任何形式的剽窃行为
转载原创文章请注明,转载自:security的博客
本文学习自DT课堂原名颜群
JDBC
- JDBC:Java DataBase Connectivity
可以为多种不同关系型数据库提供统一访问方式,用Java操作数据库
- 架构:
- jdbc接口、方法、类:API
JDBC API主要功能
-
DriverManger:管理驱动
-
Connection:连接(通过DriverManager产生)
-
Statement(PreparedStatement):增删改查(通过Connection产生)
-
CallableStatement:调用数据库中的存储过程/存储函数(通过Connection产生)
-
ResultSet:返回的结果集(上面的Statement等产生)
- Connection产生操作数据库的对象:
- Connection产生Statement对象:createStatement()
- Connection产生PreparedStatement对象:prepareStatement()
- Connection产生CallableStatement对象:prepareCall()
- Statement操作数据库:
- 增删改:executeUpdate()
- 查:executeQuery()
- PreparedStatement操作数据库(与Statement操作数据库不同的是SQL语句需要放在前面,执行时不需要SQL,因为之前已经预编译)
- 增删改:executeUpdate()
- 查:executeQuery()
- 赋值操作:set...()先用?充当占位符,再用set..更改?
- 推荐使用PreparedStatement:原因如下:
SQL注入:将客户输入的内容和开发人员的SQL语句混为一体 Statement:存在被SQL注入的风险 (例如输入用户名:任意值 ' or 1=1 -- 密码:任意值)
- CallableStatement:调用存储过程、存储函数
- connection.prepareCall(参数:存储过程或存储函数名)
- 参数格式:
- ResultSet:保存结果集
- next():光标下移判断是否有下一条数据,true/false
- previous():true/false
- getXXX(字段名|位置):获取具体的字段值
JDBC访问数据库的具体步骤
JDBCDemo代码
import java.sql.*;
public class JDBCDemo {
private static final String URL="jdbc:mysql://localhost:3306/学生作业管理数据库?serverTimezone=UTC";
private static final String USERNAME="root";
private static final String PWD="1998";
/*
public static void update() { //增删改
Connection con =null;
Statement stmt = null;
try {
//1. 导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");
//2. 与数据库建立连接
con= DriverManager.getConnection(URL,USERNAME,PWD);
//3. 发送sql,执行(增删改、查)
stmt=con.createStatement();
String sql="INSERT INTO 学生表 (学号,姓名,性别,专业班级,出生日期,联系电话) "
+ "VALUES(0538,'于兰兰','女','生物05','1984-2-20', '1331200××××');";
//4. 执行SQL
int count = stmt.executeUpdate(sql);
//5. 处理结果
if (count>0)
{
System.out.println("操作成功");
}
}
catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
finally{
try{
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
*/
public static void query() { //查询
Connection con =null;
Statement stmt = null;
ResultSet rs=null;
try {
//1. 导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");
//2. 与数据库建立连接
con= DriverManager.getConnection(URL,USERNAME,PWD);
//3. 发送sql,执行(查)
stmt=con.createStatement();
String sql="select * from 学生表";
//4. 执行SQL
rs = stmt.executeQuery(sql); //executeUpdate改为executeQuery
//5. 处理结果
while(rs.next()) {
int sno =rs.getInt("学号");
String sname =rs.getString("姓名");
System.out.println(sno+"--"+sname);
}
}
catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
// update();
query();
}
}
数据库驱动
- Oracle
- MySQL
- SqlServer
- 使用jdbc操作数据库时,如果对数据库进行了更换,只需要替换:
驱动、驱动类、连接字符串、用户名、密码
处理CLOB/BLOB类型
- 存放稍大型数据:
- CLOB(Oracle叫法,mysql叫法为TEXT)字符流:大文本数据(小说->数据)
varchar2:4000byte
- BLOB字节流:二进制文件(一切文件、图片,音频)
总结
本文最初由security发布于security的博客,禁止任何形式的剽窃行为
转载原创文章请注明,转载自:security的博客
相关文章
暂无相关文章
可以为多种不同关系型数据库提供统一访问方式,用Java操作数据库
-
DriverManger:管理驱动
-
Connection:连接(通过DriverManager产生)
-
Statement(PreparedStatement):增删改查(通过Connection产生)
-
CallableStatement:调用数据库中的存储过程/存储函数(通过Connection产生)
-
ResultSet:返回的结果集(上面的Statement等产生)
- Connection产生操作数据库的对象:
- Connection产生Statement对象:createStatement()
- Connection产生PreparedStatement对象:prepareStatement()
- Connection产生CallableStatement对象:prepareCall()
- Statement操作数据库:
- 增删改:executeUpdate()
- 查:executeQuery()
- PreparedStatement操作数据库(与Statement操作数据库不同的是SQL语句需要放在前面,执行时不需要SQL,因为之前已经预编译)
- 增删改:executeUpdate()
- 查:executeQuery()
- 赋值操作:set...()先用?充当占位符,再用set..更改?
- 推荐使用PreparedStatement:原因如下:
SQL注入:将客户输入的内容和开发人员的SQL语句混为一体 Statement:存在被SQL注入的风险 (例如输入用户名:任意值 ' or 1=1 -- 密码:任意值)
- CallableStatement:调用存储过程、存储函数
- connection.prepareCall(参数:存储过程或存储函数名)
- 参数格式:
- ResultSet:保存结果集
- next():光标下移判断是否有下一条数据,true/false
- previous():true/false
- getXXX(字段名|位置):获取具体的字段值
JDBC访问数据库的具体步骤
JDBCDemo代码
import java.sql.*;
public class JDBCDemo {
private static final String URL="jdbc:mysql://localhost:3306/学生作业管理数据库?serverTimezone=UTC";
private static final String USERNAME="root";
private static final String PWD="1998";
/*
public static void update() { //增删改
Connection con =null;
Statement stmt = null;
try {
//1. 导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");
//2. 与数据库建立连接
con= DriverManager.getConnection(URL,USERNAME,PWD);
//3. 发送sql,执行(增删改、查)
stmt=con.createStatement();
String sql="INSERT INTO 学生表 (学号,姓名,性别,专业班级,出生日期,联系电话) "
+ "VALUES(0538,'于兰兰','女','生物05','1984-2-20', '1331200××××');";
//4. 执行SQL
int count = stmt.executeUpdate(sql);
//5. 处理结果
if (count>0)
{
System.out.println("操作成功");
}
}
catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
finally{
try{
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
*/
public static void query() { //查询
Connection con =null;
Statement stmt = null;
ResultSet rs=null;
try {
//1. 导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");
//2. 与数据库建立连接
con= DriverManager.getConnection(URL,USERNAME,PWD);
//3. 发送sql,执行(查)
stmt=con.createStatement();
String sql="select * from 学生表";
//4. 执行SQL
rs = stmt.executeQuery(sql); //executeUpdate改为executeQuery
//5. 处理结果
while(rs.next()) {
int sno =rs.getInt("学号");
String sname =rs.getString("姓名");
System.out.println(sno+"--"+sname);
}
}
catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
// update();
query();
}
}
数据库驱动
- Oracle
- MySQL
- SqlServer
- 使用jdbc操作数据库时,如果对数据库进行了更换,只需要替换:
驱动、驱动类、连接字符串、用户名、密码
处理CLOB/BLOB类型
- 存放稍大型数据:
- CLOB(Oracle叫法,mysql叫法为TEXT)字符流:大文本数据(小说->数据)
varchar2:4000byte
- BLOB字节流:二进制文件(一切文件、图片,音频)
总结
本文最初由security发布于security的博客,禁止任何形式的剽窃行为
转载原创文章请注明,转载自:security的博客
相关文章
暂无相关文章
import java.sql.*;
public class JDBCDemo {
private static final String URL="jdbc:mysql://localhost:3306/学生作业管理数据库?serverTimezone=UTC";
private static final String USERNAME="root";
private static final String PWD="1998";
/*
public static void update() { //增删改
Connection con =null;
Statement stmt = null;
try {
//1. 导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");
//2. 与数据库建立连接
con= DriverManager.getConnection(URL,USERNAME,PWD);
//3. 发送sql,执行(增删改、查)
stmt=con.createStatement();
String sql="INSERT INTO 学生表 (学号,姓名,性别,专业班级,出生日期,联系电话) "
+ "VALUES(0538,'于兰兰','女','生物05','1984-2-20', '1331200××××');";
//4. 执行SQL
int count = stmt.executeUpdate(sql);
//5. 处理结果
if (count>0)
{
System.out.println("操作成功");
}
}
catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
finally{
try{
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
*/
public static void query() { //查询
Connection con =null;
Statement stmt = null;
ResultSet rs=null;
try {
//1. 导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");
//2. 与数据库建立连接
con= DriverManager.getConnection(URL,USERNAME,PWD);
//3. 发送sql,执行(查)
stmt=con.createStatement();
String sql="select * from 学生表";
//4. 执行SQL
rs = stmt.executeQuery(sql); //executeUpdate改为executeQuery
//5. 处理结果
while(rs.next()) {
int sno =rs.getInt("学号");
String sname =rs.getString("姓名");
System.out.println(sno+"--"+sname);
}
}
catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
// update();
query();
}
}
数据库驱动
- Oracle
- MySQL
- SqlServer
- 使用jdbc操作数据库时,如果对数据库进行了更换,只需要替换:
驱动、驱动类、连接字符串、用户名、密码
处理CLOB/BLOB类型
- 存放稍大型数据:
- CLOB(Oracle叫法,mysql叫法为TEXT)字符流:大文本数据(小说->数据)
varchar2:4000byte
- BLOB字节流:二进制文件(一切文件、图片,音频)
总结
本文最初由security发布于security的博客,禁止任何形式的剽窃行为
转载原创文章请注明,转载自:security的博客
相关文章
暂无相关文章
驱动、驱动类、连接字符串、用户名、密码
- 存放稍大型数据:
- CLOB(Oracle叫法,mysql叫法为TEXT)字符流:大文本数据(小说->数据)
varchar2:4000byte - BLOB字节流:二进制文件(一切文件、图片,音频)
总结
本文最初由security发布于security的博客,禁止任何形式的剽窃行为
转载原创文章请注明,转载自:security的博客
相关文章
暂无相关文章
本文最初由security发布于security的博客,禁止任何形式的剽窃行为
转载原创文章请注明,转载自:security的博客
相关文章
- 暂无相关文章
用户点评