发表于: 2018-01-05 23:48:21

1 513


一。今天做完了小课堂的PPT,

准备讲JDBC的流程:1.始初化驱动,2.通过Connection建立与数据库的连接,3.通过statement,PreparedStatement执行SQL语句,4.通过setObject方法传入数据,5.通过execute,executeUpdate,executeQuery执行更新。

问题1.:statement和preparedstatement的区别是什么?

statement每次执行sql语句,相关数据库都要执行sql语句的编译,preparedstatement是预编译的,preparedstatement支持批处理,见下图代码:

package jdbc;

import java.sql.*;

public class TestJDBC4 {
public static void main(String[] args){
try {
Class.forName("com.mysql.jdbc.Driver");
       } catch (ClassNotFoundException e){
e.printStackTrace();
       }
String sql = "insert into hero values(null,?,?,?)";
       try (Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/how2java","root","1234");
            Statement s = c.createStatement();
            PreparedStatement ps = c.prepareStatement(sql);) {
for (int i = 0; i < 10; i++) {
// Statement需要进行字符串拼接,可读性和维修性比较差
               String sql0 = "insert into hero values(null," + "'冰女'" + ","
                       + 253.0f + "," + 40 + ")";
               s.execute(sql0);
           }
// s.close();
         //  for (int i = 0; i < 10; i++) {
               // PreparedStatement 使用参数设置,可读性好,不易犯错
               // "insert into hero values(null,?,?,?)";
               ps.setString(1, "冰女");
               ps.setFloat(2, 253.0f);
               ps.setInt(3, 40);
               ps.execute();
           
       } catch (SQLException e) {
e.printStackTrace();
       }
}
}


2.PreparedStatement的setObject的作用和setString的作用是一样的!,任何方法都可以用setObject代替。

3。execute,executeUpdate,executeQuery三种方法有什么区别?

  Statement 接口提供了三种执行 SQL 语句的方法:executeQuery、executeUpdate 和 execute。使用哪一个方法由 SQL 语句所产生的内容决定。

       方法executeQuery 

       用于产生单个结果集的语句,例如 SELECT 语句。 被使用最多的执行 SQL 语句的方法是 executeQuery。这个方法被用来执行 SELECT 语句,它几乎是使用最多的 SQL 语句。

       方法executeUpdate

       用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQL DDL(数据定义语言)语句,例如 CREATE TABLE 和 DROP TABLE。INSERT、UPDATE 或 DELETE 语句的效果是修改表中零行或多行中的一列或多列。executeUpdate 的返回值是一个整数,指示受影响的行数(即更新计数)。对于 CREATE TABLE 或 DROP TABLE 等不操作行的语句,executeUpdate 的返回值总为零。

方法execute:

       execute方法仅在语句能返回多个ResultSet对象、多个更新计数或ResultSet对象与更新计数的组合时使用。


execute和executeUpdate的详细区别见下方代码:

package jdbc;
import java.sql.*;
public class TestJDBCex {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
       } catch (ClassNotFoundException e) {
e.printStackTrace();
       }
try (Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/how2java","root", "1234");
            Statement s = c.createStatement();) {
// 不同1:execute可以执行查询语句
           // 然后通过getResultSet,把结果集取出来
           String sqlSelect = "select * from hero";
           s.execute(sqlSelect);
           ResultSet rs = s.getResultSet();
           while (rs.next()) {
System.out.println(rs.getInt("id"));
           }
// executeUpdate不能执行查询语句
           // s.executeUpdate(sqlSelect);
           // 不同2:
           // execute返回boolean类型,true表示执行的是查询语句,false表示执行的是insert,delete,update等等
           boolean isSelect = s.execute(sqlSelect);
           System.out.println(isSelect);
           // executeUpdate返回的是int,表示有多少条数据受到了影响
           String sqlUpdate = "update Hero set hp = 300 where id < 100";
           int number = s.executeUpdate(sqlUpdate);
           System.out.println(number);
           } catch (SQLException e) {
// TODO Auto-generated catch block
           e.printStackTrace();
       }
}


二。看了于博韬师兄的小课堂。


三。在进行明天小课堂的代码准备工作。

向数据库中插入10000条数据,分别使用Statement和PreparedStatement,比较各自花的时间差异

可以看到,使用PreparedStatement的时候,用时明显短于使用Statement。

四。复习了DAO(DataAccess Object

package jdbc;

import java.util.List;

public interface DAO {
public void add(Hero hero);
   //修改
   public void update(Hero hero);
   //删除
   public void delete(int id);
   //获取
   public Hero get(int id);
   //查询
   public List<Hero> list();
   //分页查询
   public List<Hero> list(int start, int count);
}



明天的计划:查缺补漏一下小课堂的PPT,试着预演一下小课堂

遇到的问题:基本解决了

今天的收获:重新学习了下JDBC,又有新感觉

java任务一开始时间:2017.12.05

预计demo时间:2018.01-05

可能有延期风险,原因是:基础太差,很多任务的教程都卡壳,进行不下去。

禅道链接地址:http://task.ptteng.com/zentao/project-task-501.html



返回列表 返回列表
评论

    分享到