一.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. so so.*.*

    转自:http://unix.stackexchange.com/questions/5719/linux-gnu-gcc-ld-version-scripts-and-the-elf-binary- ...

  2. [线段树]HDU-1754板子题入门ver

    HDU-1754 线段树数组请开到四倍 众所周知数组开小会导致re tle wa等一系列问题orz 板子就是板子,数组从零开始或是从一开始都没什么问题,就是2*root+1还是2*root+2的问题. ...

  3. Intellij Idea 解决字符乱码、设定颜色主题、字体

    1.写入字符编码设置 2.调整mintty 字体.主题

  4. mysql5.7.20 windows 解压缩版安装

    1.下载 文件下载路径:https://dev.mysql.com/downloads/mysql/ 2.配置文件 在解压的文件夹内新建my.ini文件,并加入以下内容: [mysql] # 设置my ...

  5. Database First/Code First

  6. 本地存储localStorage sessionStorage 以及 session 和cookie的对比和使用

    cookie和session都是用来跟踪浏览器用户身份的会话方式. 1.验证当前服务中继续请求数据时,哪些缓存数据会随着发往服务器? 只有cookie中设置的缓存数据会发送到服务器端 2. 强调几点: ...

  7. 小程序中input设置宽度后宽度还有空间,但是placeholder被遮挡问题

    最近在做小程序,已经设置了宽高,placeholder没有超出input宽度,却被挡住了一部分,上代码看一下: wxml: <view class='container'> <inp ...

  8. Adobe Flash Player 29.0.0.140官方正式版

    Adobe Flash Player 29 正式版例行更新,v29.0.0.140 这是最新详细版本号,Adobe采取和微软相似的更新策略,在每个月的第二个周二为产品发布安全更新.Adobe® Fla ...

  9. shell练习题3

    需求如下: 请按照这样的日期格式(xxxx-xx-xx)每天生成一个文件,例如今天生成的文件为2018-10-19.log, 并把磁盘的使用情况入到这个文件,(不需要写cron,写脚本即可) 参考解答 ...

  10. 分布式session个人理解浅谈

    在分布式中,用户的session如何处理呢? 服务器中的原生session是无法满足需求的,因为用户的请求有可能随机落入到不同的服务器中,这样的结果将会导致用户的session丢失,传统做法中有解决方 ...