目录
2、调用executeQuery(sql)的结果集的ResultSet对象并完成遍历
ResultsetMetaData------获取Result结果集中列相关的信息
[2、Callable Statement ---------这是用来调用存储过程的对象](#2、Callable Statement ---------这是用来调用存储过程的对象)
[测试函数:把List>类型转成List Dept类型](#测试函数:把List>类型转成List Dept类型)
删除链接部分:面试必考:手写一个JDBC
①加载数据库驱动类
java
Class.forName("oracle.jdbc.driver.OracleDriver");//加载oracle数据库的驱动类
②获取数据库连接的connection
java
Connection conn = DriverManager
.getConnection("jdbc:oracle:thin:@192.168.77.100:1521:helowin"
,"scott","123456");//获取链接
③获取操作sql语句的Statement对象
java
Statement stat= conn.createStatement();
④如果是查询语句,调用executeQuery(sql)的结果集的ResultSet对象并完成遍历
⑤关闭ResultSet,Statement,connection对象
1、八股部分:
获取链接部分:
java
Connection conn = null;//表示数据库的连接
Statement stat= null;//用来操作SQL语句的操作
ResultSet reset =null;//用来接收查询语句返回的结果集
Class.forName("oracle.jdbc.driver.OracleDriver");//加载oracle数据库的驱动类
conn = DriverManager
.getConnection("jdbc:oracle:thin:@192.168.77.100:1521:helowin"
,"scott","123456");//获取数据库的连接
stat=conn.createStatement(); //获取statement对象
删除链接部分:
java
if (reset != null) {
reset.close();
}
if (stat != null) {
stat.close();
}
if (conn != null) {
conn.close();
}
二、查询:executeQuery(sql)的结果集
方法1:
java
String sql="select t.*, t.rowid from DEPT t";//sql保存的是查表语句
reset=stat.executeQuery(sql);//执行查询并返回结果集reset
while (reset.next()){
int deptno =reset.getInt("DEPTNO");
String dname =reset.getString("DNAME");
String loc =reset.getString("LOC");
System.out.println(deptno + "-------" + dname + "-------" + loc);
}
方法2:
ResultsetMetaData------获取Result结果集中列相关的信息
getColumnCount()------获取列数
getColumnName()------获取列名
java
String sql="select t.*, t.rowid from DEPT t";//sql保存的是查表语句
reset = stat.executeQuery(sql);//执行查询并返回结果集
rsmd = reset.getMetaData();
int columnCount = rsmd.getColumnCount();
while (reset.next()) {
for (int i = 1; i <= columnCount; i++) {
System.out.print(rsmd.getColumnName(i) +
"=" + resset.getObject(i));
}
System.out.println();
}
三、增删改
1、executeUpdate(SQL)
可以进行增删改数组,返回int型的值,表示成功增删改的条数
例1:增(插入一条记录)
java
String sql = "insert into dept values('61','运维部','南京')";
int i =stat.executeUpdate(sql);
System.out.println("成功编辑了"+i+"条记录");
例2:改(更新一条记录)
java
String sql = "update dept set dname='测试部',loc='上海'where deptno='53'";
int i =stat.executeUpdate(sql);
System.out.println("成功编辑了"+i+"条记录");
例3:删除一条记录:
java
String sql = "delete from dept where deptno='53'";
int i =stat.executeUpdate(sql);
System.out.println("成功编辑了"+i+"条记录");
2、Callable Statement ---------这是用来调用存储过程的对象
我们使用Connection对象调用prepareCall("{call存储过程名(?,?......)}")
来获取该对象
我们可以使用该对象.SetXxx(序号.参数值)来传参
我们可以使用该对象.execute()来结束对存储过程的调用
①首先在数据库中写入过程my_dept_ins
sql
create or replace procedure my_dept_ins(
v_deptno number,
v_dname dept.dname%type,
v_loc dept.loc%type)
is
begin
insert into dept values(v_deptno,v_dname,v_loc);
commit;
end;
java
CallableStatement stat= null;
stat = conn.prepareCall("{call 过程名(?,?,?)}");
stat.setInt(1,62);
stat.setString(2,"行政部");
stat.setString(3,"北京");
stat.execute();
四、基于以上结构的拆分:
能够增删改查的函数
函数1:获取链接:getSource
java
private static void getSource() throws SQLException, ClassNotFoundException {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=
DriverManager.getConnection("jdbc:oracle:thin:@192.168.77.100:1521:helowin"
,"scott","123456");
stat=conn.createStatement();
}
函数2:关闭资源:closeSource
java
private static void closeSource() throws SQLException {
if(rest !=null)
{
rest.close();
}
if(stat !=null)
{
stat.close();
}
if(conn !=null)
{
conn.close();
}
}
函数3:读取并存储(查询语句)
java
public static List<Map<String, Object>> queryBySQL(String sql) throws SQLException, ClassNotFoundException {//输入String类型的sql语句
List<Map<String, Object>> list = new ArrayList<>();
getSource();//获取链接
rest = stat.executeQuery(sql);
ResultSetMetaData rsmd = rest.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rest.next()) {
Map<String, Object> map = new HashMap<>();
for (int i = 1; i <= columnCount ; i++) {
String columnName = rsmd.getColumnName(i);
Object columnValue = rest.getObject(i);
map.put(columnName, columnValue);
}
list.add(map);
}closeSource();
return list;
函数4:增删改函数
java
public static int updateBySql(String sql) throws SQLException, ClassNotFoundException {
getSource();
int res=0;
res =stat.executeUpdate(sql);
closeSource();
return "成功编辑了"+res+"条记录";
}
类Dept:
java
import java.math.BigDecimal;
public class Dept {
BigDecimal deptno;
String dname;
String loc;
public Dept(BigDecimal deptno, String dname, String loc) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public Dept() {
}
public BigDecimal getDeptno() {
return deptno;
}
public void setDeptno(BigDecimal deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Dept{" +
"deptno=" + deptno +
", dname='" + dname + '\'' +
", loc='" + loc + '\'' +
'}';
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
测试函数:把List<Map<String,Object>>类型转成List Dept类型
java
public static void main(String[] args) throws SQLException, ClassNotFoundException {
String sql ="select * from Dept";
List<Map<String, Object>> list =JDBCUtill.queryBySQL(sql);
List<Dept> depts =new ArrayList<>();
for(Map<String,Object> map:list){
BigDecimal deptno =(BigDecimal) map.get("DEPTNO");//转类型
String dname=(String) map.get("DNAME");
String loc=(String) map.get("LOC");
Dept dept =new Dept(deptno,dname,loc);
depts.add(dept);
}
for (Dept d:depts){
System.out.println(d);
}
}
}
list里面存的元素是一个个小map,遍历这个List并且通过函数把它读到新的这个以Dept集合为元素的新的list中,然后,再遍历,(注意这边要重写dept的方法)
补充:map的函数
Map类的实例名.get(键名) 可以得到键值
例1:从数据库中读出数据
java
import javax.xml.transform.Result;
import java.sql.*;
import javax.xml.transform.Result;
import java.sql.*;
public class Test1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;//表示数据库的连接
Statement stat= null;//用来操作SQL语句的操作
ResultSet reset =null;//用来接收查询语句返回的结果集
Class.forName("oracle.jdbc.driver.OracleDriver");//加载oracle数据库的驱动类
//获取数据库的连接
conn = DriverManager
.getConnection("jdbc:oracle:thin:@192.168.77.100:1521:helowin"
,"scott","123456");//驱动管家 scott 数据库的管理员 123456密码
stat=conn.createStatement(); //获取statement对象
String sql="select t.*, t.rowid from DEPT t";//sql保存的是查表语句
reset=stat.executeQuery(sql);//执行查询并返回结果集reset
while (reset.next()){
int deptno =reset.getInt("DEPTNO");
String dname =reset.getString("DNAME");
String loc =reset.getString("LOC");
System.out.println(deptno + "-------" + dname + "-------" + loc);}
if (reset != null) {
reset.close();
}
if (stat != null) {
stat.close();
}
if (conn != null) {
conn.close();
}
}
}
}
}
java
import javax.xml.transform.Result;
import java.sql.*;
public class Test1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;//表示数据库的连接
Statement stat = null;//用来操作SQL语句的操作
ResultSet resset = null;//用来接收查询语句返回的结果集
ResultSetMetaData rsmd = null;
Class.forName("oracle.jdbc.driver.OracleDriver");//加载oracle数据库的驱动类
//获取数据库的连接
conn = DriverManager
.getConnection("jdbc:oracle:thin:@192.168.77.100:1521:helowin"
, "scott", "123456");//驱动管家
//获取statement对象
stat = conn.createStatement();
String sql = "select t.*, t.rowid from DEPT t";
resset = stat.executeQuery(sql);//执行查询并返回结果集
rsmd = resset.getMetaData();
int columnCount = rsmd.getColumnCount();
while (resset.next()) {
for (int i = 1; i <= columnCount; i++) {
System.out.print(rsmd.getColumnName(i) + "=" + resset.getObject(i));
}
System.out.println();
}
if(resset !=null)
{
resset.close();
}
if(stat !=null)
{
stat.close();
}
if(conn !=null)
{
conn.close();
}
}
}
删除记录
java
import javax.xml.transform.Result;
import java.sql.*;
public class Test6 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;//表示数据库的连接
Statement stat = null;//用来操作SQL语句的操作
Class.forName("oracle.jdbc.driver.OracleDriver");//加载oracle数据库的驱动类
//获取数据库的连接
conn = DriverManager
.getConnection("jdbc:oracle:thin:@192.168.77.100:1521:helowin"
, "scott", "123456");//驱动管家
//获取statement对象
stat = conn.createStatement();
String sql = "delete from dept where deptno='53'";
int i =stat.executeUpdate(sql);
System.out.println("成功编辑了"+i+"条记录");
if(stat !=null)
{
stat.close();
}
if(conn !=null)
{
conn.close();
}
}
}