一.JDBC简介

  全名:java database connection

  java代码与数据库相连的工具,java>JDBC>(oracle  mysql sql server)

二.四大件

  • DriverManager:根据不同数据库,调用正确的Driver
  • Connection:由Driver产生,负责数据库的连接
  • Statement:由connection产生,负责传送SQL指令
  • ResultSet:负责接收Statement(sql语句)执行结果

三.JDBC具体步骤

  1. 安装驱动,驱动为一个jar包,mysql的驱动和oracle的驱动不同
  2. 复制到src目录下,build path>addto
  3. 加载驱动-------Class.forName("com.mysql.jdbc.Driver");
  4. 与数据库相连----------Connection con=DriverManager.getConnection(url,user,psw);(此处会发生异常)
  5. 写sql语句---------Statement st=con.createStatement();(异常)
  6. 执行sql语句-----------int rows=st.executeUpdate(sql);//更新操作:此处返回受影响的行数
  7. 关闭四大件-------st.close();con.close();

  以上为更新数据,下面的例子为插入数据,具体代码如下

public class StudentDao{
private Connection con;
private Statement st;
private ResultSet rs;
public void insert(Student s)throws SQLExp{
String sql="insert****";
con=DbUtils.getConnection();
st=con.createStatement();
int rows=st.executeUpdate(sql);
        DbUtils.close(con,st,rs);
return rows;
}
public static void close(Connection con,Statement st,ResultSet rs){
try{
if(con!=null) con.close();
if(st!=null) st.close();
if(rs!=null) rs.close();
}catch(SQLExp e){
syso(连接失败);
e.printStackTrace();
}
}
}

  另有优化的方法PreparedStatement:预编译:可有效防止sql注入

public class StudentDao{
private Connection con;
private PreparedStatement ps;//
private ResultSet rs;
public void insert(Student s)throws SQLExp{
String sql="insert into Student values(?,?,?,?)";
con=DbUtils.getConnection();
ps=con.prepareStatement();
ps.setInt(1,s.getId());
ps.setString(2,s.getName());
ps.setInt(3,s.getAge());
ps.setDouble(4,s.getScore());
int rows=st.executeUpdate(sql);
     DbUtils.close(con,st,rs);
return rows;
}
}

  关于JDBC的批处理,一般用到较少

//传入参数非一个学生,二是一个学生的List数组
for(Student s:l){
String sql="insertXXXX";
st.addBatch(sql);
}
int arr[]=st.executeBatch(); //预编译:
String sql="insert into student values(?,?,?,?)";
ps=con.preparedStatement(sql);
for(Student s:l){
ps.setInt(1,s.getId());
ps.setString(2,s.getName());
ps.addBatch(sql); //批处理
}

  test类

 

StudentDao sd=new StudentDao();
List<Student>l=new ArrayList<Student>();
Student s=new Student(1015,"zhangwenfang",54.2,"sdad");
Student s1=new Student(1004,"zhangwefsng",54.2,"sfdsad");
Student s2=new Student(1003,"zhsfdang",54.2,"gasad");
l.add(s);
l.add(s2);
l.add(s1);
try {
sd.inserts(l);
System.out.print("插入成功");
} catch (SQLException e) {
System.out.println("插入失败");
e.printStackTrace();
}

 四.关于事务

  理解银行转账的过程,要么全成功,要么全失败。你给人转账,你银行得-钱,别人得+钱

  三要素:

  • 保证Connection 是同一个
  • 关闭自动提交
  • 不要忘了手动提交

14JDBC的更多相关文章

  1. 5.14JDBC

    一.##JDBC 1. 概念:Java DataBase Connectivity  Java 数据库连接, Java语言操作数据库. JDBC本质:其实是官方(sun公司)定义的一套操作所有关系型数 ...

随机推荐

  1. vue.js 自带阻止默认事件 阻止冒泡

    <!-- 阻止单击事件冒泡 --> <a v-on:click.stop="doThis"></a>   <!-- 提交事件不再重载页面  ...

  2. Selenium自动化获取Http报文信息并判断当前API状态

    public int loadingFinishedCount(WebDriver driver){ LogEntries logs = driver.manage().logs().get(&quo ...

  3. How to find out which version of tensorflow is installed in my pc? - 如何找出我的电脑中安装了哪个版本的 tensorflow?

    I'm using tensorflow and have got some weired issues. I'm told it might be related to the version of ...

  4. Centos7 Firewall

    1.1 firewall启停设置 [root@tomcat ~]# systemctl stop firewalld.service #关闭firewall [root@tomcat ~]# syst ...

  5. 【webpack学习笔记】a06-生产环境和开发环境配置

    生产环境和开发环境的配置目标不一样,生产环境主要是让文件压缩得更小,更优化资源,改善加载时间. 而开发环境,主要是要开发更方便,更节省时间,比如调试比如自动刷新. 所以可以分开配置不同的开发环境,然后 ...

  6. 《推荐》安装Photoshop详细步骤 ,手把手,一步一步,具体详细地教你安装Photoshop (Adobe photoshop CS6)

    现在的大学生必备技能,除了单反拍照,就是PS (Photoshop)了.可以说PS是一个强大的图片编辑处理软件,也是目前公认的最好的通用平面美术设计软件,它的功能完善,性能稳定,使用也很方便.几乎在所 ...

  7. React中this.setState是同步还是异步?为什么要设计成异步?

    在使用react的时候,this.setState为什么是异步呢? 一直以来没有深思这个问题.昨天就此问题搜索了一下. react创始人之一 Dan Abramovgaearon在GitHub上回答了 ...

  8. python-之-深浅拷贝一

    深浅拷贝 一.数据为不可变类型 (str.int.bool) import copy v1 = "abc" v2 = copy.copy(v1) v3 = copy.deepcop ...

  9. PAT A1046 Shortest Distance

    PAT A1046 Shortest Distance 标签(空格分隔): PAT TIPS: 最后一个数据点可能会超时 #include <cstdio> #include <al ...

  10. 远程桌面服务当前正忙,因此无法完成您尝试执行的任务-win2008R2

    远程桌面服务当前正忙,因此无法完成您尝试执行的任务,近来我服务器出现这情况, 到达主机房看主机...不可以登陆,也没有登陆框.只能关机. 在微软找到的原因是:Csrss.exe 进程和某些应用程序 ( ...