1.连接ORACLE数据库:
连接放在同一个类的主方法中
package com.softeem.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCTest { public static void main(String[] args) { Connection conn=null; Statement st=null; ResultSet rs=null; try { //数据库连接 Class.forName("oracle.jdbc.driver.OracleDriver"); conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","system"); //数据库查询 st=conn.createStatement(); rs=st.executeQuery("SELECT * FROM users"); while(rs.next()){ String stuid=rs.getString("stuid"); String stuname=rs.getString("stuname"); System.out.println(stuid+"\t"+stuname); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { rs.close(); st.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
2.连接与操作分开:
2.1 对数据库连接与关闭的工具类
package com.ming.jdbc; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Connection; import java.sql.DriverManager; public class DBUtil { // 定义变量 private static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; private static final String url = "jdbc:oracle:thin:@localhost:1521:orcl"; private static final String username = "scott"; private static final String password = "a123"; // 加载驱动 static { try { Class.forName(DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection connection() { Connection connection = null; try { connection = DriverManager.getConnection(url, username, password); } catch (SQLException e) { e.printStackTrace(); } return connection; } public static void closeConn(Connection conn) { if (null != connection()) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } // 重载 public static void closeConn(ResultSet rs, Statement sm, Connection conn) { try { if (null != rs) { rs.close(); } if (null != sm) { sm.close(); } if (null != conn) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } }
2.2 对数据库的操作类
package com.ming.jdbc; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Connection; public class DBOperation { /** * @param sql 创建表的SQL * @return 表是否创建成功 */ public static boolean getConn(String sql){ boolean b=true; Connection conn=DBUtil.connection(); try { Statement sm=conn.createStatement(); sm.executeUpdate(sql); } catch (SQLException e) { b=false; e.printStackTrace(); }finally{ DBUtil.closeConn(conn); } return b; } //CRUD C:create R:read U:update D:delete public static boolean cudOperation(String sql){ boolean b=false; Connection conn=DBUtil.connection(); try { Statement sm=conn.createStatement(); int i=sm.executeUpdate(sql); if(i>0){ b=true; } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.closeConn(conn); } return b; } public static void query(String sql){ Connection conn=null; try { conn=DBUtil.connection(); Statement sm=conn.createStatement(); ResultSet rs=sm.executeQuery(sql); System.out.println("员工号 \t\t"+"员工姓名 \t\t"+"职位 \t\t"+""+"工资 \t\t"); while(rs.next()){ String empno=rs.getString("empno"); String ename=rs.getString("ename"); String job=rs.getString("job"); String sal=rs.getString("sal"); System.out.println(empno+"\t\t"+ename+"\t\t"+job+"\t\t"+sal); } } catch (SQLException e) { e.printStackTrace(); }finally{ DBUtil.closeConn(conn); } } }
2. 对象的序列化
1.创建学生对象
package com.softeem.bean; import java.io.Serializable; public class StudentBean implements Serializable { // 表中的列 与 bean中的字段是一一对应关系 // student 表 // student bean 传值 查询结果的封装,一条对应一个对象 // student dao 操作 // DAO data access object 面向数据库的直接操作,增删改查.... // 用户--〉100(控制台), 100-8(service 业务处理) ---> 92(dao) // 1 出纳 收集数据,计算 签字 // 2 会计 审核 签字 // 3 总经理 签字 // 银行转账 // 学号 char(3) primary key, // 姓名 char(4), // 性别 char(2), // 年龄 int, // 班级 char(5) private String stuNo; private String stuName; private String stuSex; private int stuAge; private String stuClass; public String getStuNo() { return stuNo; } public void setStuNo(String stuNo) { this.stuNo = stuNo; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public String getStuSex() { return stuSex; } public void setStuSex(String stuSex) { this.stuSex = stuSex; } public int getStuAge() { return stuAge; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } public String getStuClass() { return stuClass; } public void setStuClass(String stuClass) { this.stuClass = stuClass; } }
2.对学生对象操作
package com.softeem.jdbc; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import com.softeem.bean.StudentBean; /** * softeem.student * * @author Administrator * */ public class StudentDAO { // stu 封装数据 并传值 public static void insert(StudentBean stu) { String sqlString = "insert into student(学号,姓名,性别,年龄,班级) " + "values(?,?,?,?,?)"; Connection con = DbUtil.getConn(); try { // 预编译 PreparedStatement ps = con.prepareStatement(sqlString); ps.setString(1, stu.getStuNo()); ps.setString(2, stu.getStuName()); ps.setString(3, stu.getStuSex()); ps.setInt(4, stu.getStuAge()); ps.setString(5, stu.getStuClass()); int row = ps.executeUpdate(); System.out.println(row); } catch (SQLException e) { e.printStackTrace(); }finally{ DbUtil.close(con); } } public static ArrayList query(String stuNO){ // 无条件 select * from student String sql = "select * from student where 学号=?"; Connection con = DbUtil.getConn(); ArrayList<StudentBean> students = new ArrayList<StudentBean>(); try { PreparedStatement ps= con.prepareStatement(sql); ps.setString(1, stuNO); ResultSet rs = ps.executeQuery(); StudentBean sb = null; while(rs.next()){ sb = new StudentBean(); String stuNo =rs.getString("学号"); String stuName =rs.getString("姓名"); String stuSex =rs.getString("性别"); int stuAge =rs.getInt("年龄"); String stuClass =rs.getString("班级"); sb.setStuAge(stuAge); sb.setStuClass(stuClass); sb.setStuName(stuName); sb.setStuNo(stuNo); sb.setStuSex(stuSex); students.add(sb); } } catch (SQLException e) { e.printStackTrace(); } return students; } }
3.数据库连接池
1.简单操作
package com.softeem.dbcp; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.apache.commons.dbcp.BasicDataSource; public class DbcpDemo { // 定义常量 private static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl"; private static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; private static final String USERNAME = "softeem"; private static final String PASSWORD = "softeem"; public static BasicDataSource getDs() { // 创建一个数据源 BasicDataSource ds = new BasicDataSource(); // set ds.setDriverClassName(DRIVER); ds.setUrl(URL); ds.setUsername(USERNAME); ds.setPassword(PASSWORD); // 设置最大连接数 ds.setMaxActive(20); return ds; } public static Connection getCon() { Connection connection = null; try { connection = getDs().getConnection(); } catch (SQLException e) { e.printStackTrace(); } return connection; } // 关闭 连接 ps rs public static void close(ResultSet rs, Statement st, Connection con) { try { if (rs != null) rs.close(); if (st != null) st.close(); if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } }
2.使用单例模式
package com.softeem.dbcp; import java.sql.Connection; import java.sql.SQLException; import org.apache.commons.dbcp.BasicDataSource; public class SingleDbcp { // 定义常量 private static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl"; private static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; private static final String USERNAME = "softeem"; private static final String PASSWORD = "softeem"; private static SingleDbcp singleDbcp; // 构造器私有化 private SingleDbcp() { } public static synchronized SingleDbcp getSingleDbcp() { if (singleDbcp == null) singleDbcp = new SingleDbcp(); return singleDbcp; } public BasicDataSource getDs() { // 创建一个数据源 BasicDataSource ds = new BasicDataSource(); // set ds.setDriverClassName(DRIVER); ds.setUrl(URL); ds.setUsername(USERNAME); ds.setPassword(PASSWORD); // 设置最大连接数 ds.setMaxActive(20); return ds; } public Connection getCon() { Connection con = null; try { con = getDs().getConnection(); } catch (SQLException e) { e.printStackTrace(); } return con; } }
3.使用属性文件
3.1 写属性文件
DRIVER=oracle.jdbc.driver.OracleDriver URL=jdbc:oracle:thin:@localhost:1521:orcl USERNAME=scott PASSWORD=a123
3.2 读取属性文件
package com.softeem.dbcp; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; public class ReadProperties { public static String url; public static String driver; public static String username; public static String password; // path 如果文件在工程下面 path=文件名 private static FileInputStream fis = null; private static Properties ps = null; static{ // ctrl+shift+ y(小写 ) x(大写) try { fis = new FileInputStream("db.properties"); ps = new Properties(); ps.load(fis); url = ps.getProperty("URL"); driver = ps.getProperty("DRIVER"); username = ps.getProperty("USERNAME"); password = ps.getProperty("PASSWORD"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
3.3 用属性文件操作连接池
package com.softeem.dbcp; import java.sql.Connection; import java.sql.SQLException; import org.apache.commons.dbcp.BasicDataSource; public class SingleDbcp2 { // 定义常量 static BasicDataSource ds = null; static { // 创建一个数据源 ds = new BasicDataSource(); // set ds.setDriverClassName(ReadProperties.driver); ds.setUrl(ReadProperties.url); ds.setUsername(ReadProperties.username); ds.setPassword(ReadProperties.password); // 设置最大连接数 ds.setMaxActive(20); } public Connection getCon() { Connection con = null; try { con = ds.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return con; } }
本文出自 “ming” 博客,谢绝转载!