发表于: 2017-08-09 22:30:24
2 1138
今天完成的事
1.学习使用PreparedStatement
PreparedStatement和Stament相比的优点:
a: 代码的可读性和可维护性,虽然PreparedStatemen的代码会比Statement的代码多几行,但是可读性和可维护性都上升了几个档次,谁用谁知道
b: 可以尽可能地提高性能,用Statement对象时,每次执行一个SQL命令,都会对它进行解析编译,而PreparedStatement对象在多次执行同一个SQl语句时都只解析编译一次。PreparedStatement对象“就像一条生产线,批量生产同一型号的产品速度非常快。”这样便可极大地减少资源开销。
c:相对比较安全,可以防止sql注入攻击
PreparedStatement的增删改查
增 Insert
package jdbc2;
/**
* Created by yaheng on 2017/8/9.
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class InsertDemo {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
String sql = "insert into hero values(null,?,?,?)";
Connection c = DriverManager.
getConnection("jdbc:mysql://127.0.0.1:3306/yaheng?characterEncoding=UTF-8","root", "0123.");
PreparedStatement ps = c.prepareStatement(sql);
// 设置参数
ps.setString(1, "卡特");
ps.setFloat(2, 700);
ps.setInt(3, 30);
// 执行
ps.execute();
//关闭数据链接
ps.close();
c.close();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
删 Delete
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* Created by yaheng on 2017/8/9.
*/
public class DeleteDemo {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
String sql = "delete from hero where NAME =?";
Connection c = DriverManager.
getConnection("jdbc:mysql://127.0.0.1:3306/yaheng?characterEncoding=UTF-8", "root", "0123.");
PreparedStatement ps = c.prepareStatement(sql);
// 设置参数
ps.setString(1,"提莫");
// 执行
ps.execute();
//关闭数据链接
ps.close();
c.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
改 Update
package jdbc2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* Created by yaheng on 2017/8/9.
*/
public class UpdateDemo {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
String sql = "update hero set blood=? where id=?";
Connection c = DriverManager.
getConnection("jdbc:mysql://127.0.0.1:3306/yaheng?characterEncoding=UTF-8", "root", "0123.");
PreparedStatement ps = c.prepareStatement(sql);
// 设置参数
ps.setFloat(1, 5000);
ps.setInt(2, 1);
// 执行
ps.execute();
//关闭数据链接
ps.close();
c.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}}
查就跟增删改不一样
需要先建一个类作为查出来的数据储存载体,查出来的内容利用RS方法暂时存在这个类中,再将这个类中的数据利用System.out语句输出,就是查到的数据
先建类
package jdbc2;
/**
* Created by yaheng on 2017/8/9.
*/
public class Hero {
private int id;
private String name;
private float blood;
private int defence;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getBlood() {
return blood;
}
public void setBlood(float blood) {
this.blood = blood;
}
public int getDefence() {
return defence;
}
public void setDefence(int description) {
this.defence = defence;
}
public Hero(int id, String name, int blood, int defence) {
//super();
this.id = id;
this.name = name;
this.blood = blood;
this.defence = defence;
}
public Hero( String name, int blood, int defence) {
//super();
this.name = name;
this.blood = blood;
this.defence = defence;
}
public Hero() {
super();
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", blood" + blood
+ ", defence=" + defence+ "]";
}
}
再查数据
package jdbc2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
/**
* Created by yaheng on 2017/8/9.
*/
public class SelectDemo {
public static void main(String[] args) {
Hero h=null;
try {
Class.forName("com.mysql.jdbc.Driver");
String sql = "select blood from hero where name=?";
Connection c = DriverManager.
getConnection("jdbc:mysql://127.0.0.1:3306/yaheng?characterEncoding=UTF-8", "root", "0123.");
PreparedStatement ps = c.prepareStatement(sql);
// 设置参数
ps.setString(1,"贾克斯");
// 执行
ResultSet rs = ps.executeQuery();
if (rs.next()){
h = new Hero();
h.setBlood(rs.getFloat(1));
float i = h.getBlood();
System.out.println(i);
}
rs.close();
ps.close();
c.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
今天首次尝试优化自己的代码
将昨天学到的两个Try方法合并成一个,并在末尾添加关闭Stament和数据库链接的语句,即用即关 前后对比如下
遇到的问题:再使用Select语句时,不知道要先建承载类,在编代码的时候,不断报错,认真看看网上的实例以及和师兄探讨后,明白了承载类的作用,并使用了这个方法
明天的计划:开始编写DAO 学习使用Mybatis
收获:第一次尝试优化代码,明白了Select语句与其他语句的不同
任务进度:任务1,步骤17
开始时间:2017-8-7
已延期,正在按计划推进
评论