标签:
什么是JDBC?为什么使用JDBC
1)SUN公司为使Java程序灵活的访问各种不同的关系型数据库提供的规则。所以sum公司就给出了方法然后让各个开发数据库公司实现这些方法,这样子程序员就是使用一套规则操作所有数据库,这个就是所谓的JDBC,也就是javadatebaseconnectitify
关闭流的顺序
a)依次按resul->statement->connection顺序关闭
b)在能完成业务的情况下,尽早关闭、
result,statement:轻量级,创建和销毁rs所需要的时间和资源较小。
connection:重量级,上面要关闭的两个都是通过这个来的,如果实在不行,其他可以不管,这个一定要关闭。
JDBC的六个固定步骤
1,注册数据库驱动[利用反射]
2,取得数据库连接对象Connection
3,创建SQL对象
4,执行SQL命令,并返回结果集
5,处理结果集
6,依次关闭结果集
基于JDBC的CURD操作
属性配置文件
driver = com.mysql.jdbc.Driver url = jdbc:mysql://127.0.0.1:3306/mydb2 user = root password =rootJDBC工具类
package cn.itcast.web.jdbc; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Properties; //JDBC工具类:关闭流和取得连接 public final class JdbcUtil { private static String driver; private static String url; private static String user; private static String password; //静态块:加载文件 static{ Properties props = new Properties(); InputStream is = Crud.class.getClassLoader().getResourceAsStream("cn/itcast/web/jdbc/db.properties"); //其实也可以不用demo3,而用this,但是因为这里是static,所以就没有this try { props.load(is); } catch (Exception e) { e.printStackTrace(); } driver = props.getProperty("driver"); url = props.getProperty("url"); user = props.getProperty("user"); password = props.getProperty("password"); } //静态块:注册驱动 static{ try { Class.forName(driver); } catch (Exception e) { e.printStackTrace(); } } //取得连接 public static Connection getMySqlConnection(){ Connection conn = null; try { conn = DriverManager.getConnection(url,user,password); } catch (Exception e) { e.printStackTrace(); } return conn; } //关闭连接 public static void close(ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (Exception e) { e.printStackTrace(); } } } public static void close(Statement stmt){ if(stmt!=null){ try { stmt.close(); } catch (Exception e) { e.printStackTrace(); } } } public static void close(Connection conn){ if(conn!=null){ try { conn.close(); } catch (Exception e) { e.printStackTrace(); } } } }CRUD操作
package cn.itcast.web.jdbc; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import org.junit.Test; //基于JDBC的CURD操作 public class Crud { @Test public void create(){ Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = "insert into user(name,gender,salary) values(‘丝丝‘,‘女‘,3000)"; try { conn = JdbcUtil.getMySqlConnection(); stmt = conn.createStatement(); int i = stmt.executeUpdate(sql); //返回的是印象的行数 System.out.println(i>0?"成功":"失败"); } catch (Exception e) { e.printStackTrace(); }finally{ JdbcUtil.close(rs); JdbcUtil.close(stmt); JdbcUtil.close(conn); } } @Test public void read(){ Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = "select * from user where gender=‘male‘"; try { conn = JdbcUtil.getMySqlConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while(rs.next()){ String name = rs.getString("name"); String gender = rs.getString("gender"); System.out.println(name+":"+gender); } } catch (Exception e) { e.printStackTrace(); }finally{ JdbcUtil.close(rs); JdbcUtil.close(stmt); JdbcUtil.close(conn); } } @Test public void update(){ Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = "update user set gender=‘female‘ where id = 8"; try { conn = JdbcUtil.getMySqlConnection(); stmt = conn.createStatement(); int i = stmt.executeUpdate(sql); //返回的是影响到的行数,也就是有多少记录 System.out.println(i>0?"成功":"失败"); } catch (Exception e) { e.printStackTrace(); }finally{ JdbcUtil.close(rs); JdbcUtil.close(stmt); JdbcUtil.close(conn); } } @Test public void delete(){ Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = "delete from user where salary IS NULL"; try { conn = JdbcUtil.getMySqlConnection(); stmt = conn.createStatement(); int i = stmt.executeUpdate(sql); System.out.println(i>0?"成功":"失败"); } catch (Exception e) { e.printStackTrace(); }finally{ JdbcUtil.close(rs); JdbcUtil.close(stmt); JdbcUtil.close(conn); } } @Test public void read(String name){ Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = "select * from user where name=‘"+name+"‘"; System.out.println("sql="+sql); try { conn = JdbcUtil.getMySqlConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while(rs.next()){ //while(rs!=null) name = rs.getString("name"); String gender = rs.getString("gender"); System.out.println(name+":"+gender); } } catch (Exception e) { e.printStackTrace(); }finally{ JdbcUtil.close(rs); JdbcUtil.close(stmt); JdbcUtil.close(conn); } } public void createTable(String tableName){ } public void dropTable(String tableName){ } public static void main(String[] args) { Crud crud = new Crud(); //crud.read(" ‘or true or‘ "); crud.createTable("system"); crud.dropTable("system"); } }
内容补充
标签:
原文地址:http://my.oschina.net/u/2356176/blog/467076