JDBC的使用
JDBC六大步骤
(1)注册驱动
第一种方式:使用反射 Class.forName(“数据库名字”)
Class.forName("oracle.jdbc.driver.OracleDriver");
第二种方式:创建Driver对象
Driver driver = new oracle.jdbc.driver.OracleDriver;
DriverManager.deregisterDriver(driver);
第三种方式:使用系统参数jdbc.driver
//第一种:写到Run As-->Run configurations-->
//Arguments-->VM arguments中
-Djdbc.driver=oracle.jdbc.driver.OracleDriver
//第二种在代码中直接写
System.set("jdbc.driver","oracle.jdbc.driver.OracleDriver");
(2)连接数据库
第一种方式:创建DriverManager对象使用getConnection()方法
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","briup","briup");
//这个方法底层还是调用Driver中的conncet方法来实现
第二种方式:使用Driver中的connect()方法 (需要与第一步中的第二中方法连用 需要Driver对象)
//connect 方法中第一个参数是url,第二个参数是properties类型的参数,底层实际是Map集合,
//用来传入用户名和密码,需要键分别为user,password
//所以new一个properties放入用户名密码
Properties info = new Properties();
info.setProperty("user","briup");
info.setProperty("password","briup");
Conncetion conn=driver.connect("jdbc:oracle:thin:@localhost:1521:XE",info);
(3)创建statement对象
第一种方式:statement(异构)
Statement st = conn.createStatement();
第二种方式:preparedStatement(同构)
PreparedStatement pst = conn.preparedStatement();
(4)执行sql语句
String sql="";
st.execute(sql);//无返回结果时使用
st.executeQuery(sql);//查询语句 返回RuseltSet
st.executeUpdate(sql);//DML语句
(5)处理结果集
while(st.next()){
}//针对查询语句
(6)关闭资源
先开后关,在finally块中,注意判断是否为空
使用JDBC
创建一张表t_student(id,name,age)
create table t_student(
id number primary key,
name varchar2(100) not null,
age number,
birthday date
);
通过jdbc完成以下要求:
1) 向表中循环插入10条记录
2) 将id=2的名字该为张三
3) 删除id=5的记录
4) 查询所有记录信息,打印在控制台上
package com.briup.ex;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCday1 {
//创建t_student表
public static void createTable() {
Connection conn =null;
Statement st = null;
try {
//第一步:注册驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//第二步:连接数据库
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","briup","briup");
//第三步:创建一个Statement对象
st = conn.createStatement();
//第四步:执行sql语句
//1)要执行的sql语句
String sql="create table t_student("
+ " id number primary key,"
+ "name varchar2(100) not null,"
+ "age number,"
+ "brithday date)";
//2)执行sql语句获取返回值
boolean b = st.execute(sql);
System.out.println(b);
//无结果集
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
//第六步:关闭资源
if(st!=null) st.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//插入数据
public static void insertTable() {
Connection conn=null;
Statement st =null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver") conn=DriverManager.getConnection("jbdc:oracle:thin:@localhost:1521:XE","briup","briup");
st=conn.createStatement();
int i=2;
while(i<=10) {
String sql="insert into t_student"
+ " values("+i+",'jack',20,'09-9月-20')";
i++;
int j = st.executeUpdate(sql);
System.out.println("插入"+j+"行");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(st!=null) st.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//修改属性
public static void updateTable() {
Connection conn=null;
Statement st =null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jbdc:oracle:thin:@localhost:1521:XE","briup","briup");
st=conn.createStatement();
String sql="update t_student"
+ " set name='张三'"
+ " where id=2";
int j = st.executeUpdate(sql);
System.out.println("已修改"+j+"行");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(st!=null) st.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//删除数据
public static void deleteTable() {
Connection conn=null;
Statement st =null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jbdc:oracle:thin:@localhost:1521:XE","briup","briup");
st=conn.createStatement();
String sql="delete from t_student"
+ " where id=5";
int j = st.executeUpdate(sql);
System.out.println("已删除"+j+"行");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(st!=null) st.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//查询业务
public static void selectTable() {
Connection conn=null;
Statement st =null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jbdc:oracle:thin:@localhost:1521:XE","briup","briup");
st=conn.createStatement();
String sql="select id,name,age,brithday from t_student";
ResultSet set = st.executeQuery(sql);
while(set.next()) {
int id = set.getInt("id");
String name = set.getString("name");
int age = set.getInt("age");
Date brithday = set.getDate("brithday");
System.out.println("id:"+id+"\tname:"+name+"\tage:"+age+"\tbrithday:"+brithday);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(st!=null) st.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
createTable();
insertTable();
updateTable();
deleteTable();
selectTable();
}
}
本文详细介绍了如何使用JDBC连接Oracle数据库,包括JDBC的六大步骤:注册驱动、连接数据库、创建Statement对象、执行SQL语句、处理结果集以及关闭资源。并提供了创建表、插入数据、更新数据、删除数据和查询数据的具体示例。
1万+

被折叠的 条评论
为什么被折叠?



