JDBC
1.JDBC基础知识
1.1 JDBC获取连接四要素
①驱动名
②url
③账号
④密码
2.JDBC操作
2.1基本操作
2.1.1关于JavaSE
①成员变量 的位置 不带null 引用类型会默认=null;
而局部变量 (方法中) 则是需要写出null(如 增删改查方法中为了最后释放资源 需在局部变量位置上放 Connection connection =null; PreparedStatement statement=null; ResultSet rs=null;)
②关于static:
1)static 修饰的变量表示所有对象共用 ,没有 staitc 则是每个对象 特有
2)加了static 的变量 则是从静态方法区中拿 没加static的变量 对象从堆内存中获取
2.1.2 查看源码时还未导入
快捷键 ctrl+shift + t 查看具体的类
step1
step2
step3
2.1.3 如何检测握手成功(show processlist命令)
step1
step2
step3
step4
2.1.4 Iterator和ResultSet遍历时的区别
Iterator的判断和取出下一个值时,是两个方法:判断:hasNext() 和 取出:next();
ResultSet的判断和取出时同一个方法:next() 需注意的是 它的光标刚开始是在第一行数据前 并且它next()判断有数据后返回true并拿到该条数据 可通过getXxx(String column)获取
2.1.5 Statement 和PreparedStatement区别
- 方法上的区别 Statement 的方法有:
boolean execute (String sql);
ResultSet executeQuery(String sql);用于查
void executeUpdate(String sql);用于增删改 - preparedStatement的优点:
① sql语句的拼接更加简便,可维护性更高
② PreparedStatement的效率更高 有些数据库服务器(DBMS)中有预编译池
此功能mysql没有 而 oracle 有
③可以防止sql注入 (登录案例)
结论 : PreparedStatement 比 Statement 安全.可防止 SQL 注入.
Statement 操作中是把==’ OR 1=1 OR '== 拼接到sql 字符串中从而修改了sql 的原本语义.
SELECT * FROM t_user WHERE username = ‘’ OR 1=1 OR ‘’ AND password = ‘afasdfsa’
PreparedStatement 是一开始就把sql 的语义定义下来了.’ OR 1=1 OR '仅仅是作为 username 的值来查询而已.
2.1.6 properties文件改编码格式
step1
step2
2.1.7 两种导入jar包的方式
方式一:加入到user library 后再添加
step1
step2
step3
step4
step5
step6
方式二: 直接复制进folder 再add 进classpath
两种的差别是 第二种可能误删奶瓶 和 folder 文件下的jar 其一 从而引起冲突 解决方案 见 下面异常的 "ClassNotFound测试类 "
2.2Demo
public class StudentDaoImpl implements IStudentDao{
@Override
public void save(Student student) {
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection = JdbcUtil.getConnection();
String sql="insert into t_student values (null,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, student.getName());
preparedStatement.setString(2, student.getEmail());
preparedStatement.setInt(3,student.getAge());
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JdbcUtil.close(connection,preparedStatement, null);
}
}
@Override
public void delete(int id) {
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
String sql="delete from t_student where id=?";
connection = JdbcUtil.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
JdbcUtil.close(connection, preparedStatement, null); }
}
@Override
public void update(Student student) {
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
connection = JdbcUtil.getConnection();
String sql="update t_student set name=? ,email=?,age=? where id=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1, student.getName());
preparedStatement.setString(2, student.getEmail());
preparedStatement.setInt(3, student.getAge());
preparedStatement.setInt(4,student.getId());
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JdbcUtil.close(connection, preparedStatement, null);
}
}
@Override
public Student getById(int id) {
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet result=null;
try {
connection = JdbcUtil.getConnection();
String sql="select * from t_student where id=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
result = preparedStatement.executeQuery();
if(result.next()) {
String name = result.getString("name");
String email = result.getString("email");
int age = result.getInt("age");
Student student=new Student(id, name, email, age);
return student;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JdbcUtil.close(connection, preparedStatement, result);
}
return null;
}
@Override
public List<Student> getAll() {
Connection connection =null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
List<Student> list=new ArrayList<Student>();
try {
connection = JdbcUtil.getConnection();
String sql="select * from t_student";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String email = resultSet.getString("email");
int age = resultSet.getInt("age");
Student student=new Student(id, name, email, age);
list.add(student);
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JdbcUtil.close(connection, preparedStatement, resultSet);
}
return null;
}
}
Attention
- dao的实现类中的异常必须try…catch… 因为 其父类 dao 并没有抛异常
- 因为要满足jdbc规范 面对接口编程 所以导入的包都是sql 下的
- java中 BigDecimal 对应 mysql 中的decimal 类型 通常跟钱有关的都用这个类型 ,更加精确 (注意 java中 BigDecimal的参数需要放字符串 结果更加精确)
3.异常
3.1ClassNotFound
① 可能原因 : 由java文件没有自动编译
②ClassNotFound测试类
相关jar包通过复制粘贴add进library 如果删除lib下的jar包 奶瓶仍然存在 则会测试类ClassNotFound (此时project项目上有一个红色的感叹号)
step1
step2
step3
3.2红色叉叉报错
可能是编译的时候卡了一下,导致编译后的字节码文件不完整,无法正常运行。此时需要将已生成的字节码文件clean一下 重新编译