您的位置:首页 > 其它

JDBC 笔记

2017-08-02 20:45 253 查看

JDBC 笔记

JDBC 是什么?

JDBC 全称就是 Java database connection ,是 sun 公司定义的一组连接数据库的接口,具体的实现类是由各个数据库厂商提供的,称之为驱动。所以,要通过 JDBC 的 API 连接数据库时,必须要有对应的驱动支持。

下面是 JDBC 的架构图:



其中的 DriverMannager 类是 sun 公司编写的类,专门用来管理不同数据库厂商提供的驱动。

到底怎么通过 JDBC 操纵数据库呢?

首先需要下载你需要使用的数据库厂商提供的 Java 驱动包,导入项目中。然后在项目中直接编写代码就可以了。

@Test
public void test() throws Exception{
ResultSet rs = null;
Connection con = null;
PreparedStatement ps = null;
//第一步,加载驱动
Class.forName("com.mysql.jdbc.Driver");
//第二步,获得连接对象
con =  DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc","root","adminadminadmin");
//利用 preparedStatement 接口来执行 sql 语句,这里会做一个预处理。通过占位符的方式代替变量,可以有效的解决 sql 注入的问题
ps= con.prepareStatement("select id,username,password from demo1 where id > ?");
ps.setObject(1, 3);
//第三步,执行 sql 语句,查询就用 executeQuery() 方法,其他就用 executeUpdate() 方法。
rs = ps.executeQuery();
System.out.println(rs);
while(rs.next()){//遍历结果集,得到结果。
System.out.println(rs.getString(1) + "---" + rs.getString(2) + "---" + rs.getString(3));
}
}


如何封装 JDBC 代码呢?

数据库的连接是通过 Socket 实现的,网络的连接是非常占用资源的,JDBC 非常需要进行优化。而且,数据库的连接信息都不是直接写在程序中的,而是用资源文件代替。

下面就展示怎么写:封装 JDBCUtils 类

public class JDBCUtils {
//将资源文件放进 Properties 对象中,方便读取文件内容
static Properties p = null;
static {
p = new Properties();
try {       p.load(JDBCUtils.class.getResourceAsStream("/db.properties"));//此处读取资源文件存在路径的问题,/ 表示在 src 目录下。
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static Connection getMysqlConnection1() {
try {
Class.forName(p.getProperty("mysqlDriver"));
return DriverManager.getConnection(p.getProperty("mysqlURL"),p.getProperty("mysqlUsername"),p.getProperty("mysqlPassword"));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;

}

public static void close(ResultSet rs,PreparedStatement ps,Connection con) {
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("rs");
}
}
if(ps != null) {
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("ps");
}
}
if(con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("con");
}
}
}
}


调用 JDBCUtils 类封装的方法

Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = JDBCUtils.getMysqlConnection1();
ps = con.prepareStatement("select * from testdate");
rs = ps.executeQuery();
while(rs.next()) {
System.out.println(rs.getObject(1) + "->" +rs.getObject(2) + "->" + rs.getObject(3) + "->" + rs.getObject(4));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.close(rs, ps, con);
}


补充一个关于事务处理的实现

事务是通过 Connection 对象来控制的,若其中某个 sql 没有执行成功,就会采用回滚操作 rollback( ) 。

ResultSet rs = null;
Connection con = null;
PreparedStatement ps1 = null;
PreparedStatement ps2 = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获得连接对象
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc","root","adminadminadmin");
con.setAutoCommit(false);
//利用 preparedStatement 接口来执行 sql 语句,可以有效解决 sql 注入的问题,这里会做一个预处理,prepareStatement()
//方法会进行判断,sql 注入的问题直接被解决了
ps1= con.prepareStatement("insert into demo1(username,password,age) values('zh','123456','80')");
ps1.executeUpdate();
System.out.println("第一个执行成功");
ps2= con.prepareStatement("insert into demo1(username,password,aged) values('zh','123456','80')");
ps2.executeUpdate();
con.commit();
} catch (Exception e) {
//e.printStackTrace();
try {
System.out.println("回滚");
con.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}finally {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps1 != null) {
try {
ps1.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: