连接数据库,对数据进行增删查改
//连接数据库
JDbc连接数据库过程:1.加载驱动 class.forName("oracle.jdbc.driver.OracleDriver")
2.建立连接 Connection conn=DriverManager.getConnection(url,user,password);
3.创建发送Sql语句的statement对象 Statement st=conn.createStatement();
4.发送sql语句:dml:执行跟新删除和插入用语句 st.executeUpdate(sql);
select 用:Resyltset rs=st.executeQuery(sql);
5,处理Resultset对象
while(rs.next()){
....
}
6.释放对象 按照出现的顺序,反顺序进行释放
rs.close();st.close();conn.close();
package jdbc; import java.io.BufferedInputStream; import java.io.Closeable; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /*连接数据库 应用数据库配置文件 读到 数据库中登录 * Properties 类表示了一个持久的属性集。 * Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。 * * @auther:Shadow * @Date:2018.09.28 * */ public class denglu { public static void main(String[] args) { logOracle(); } public static void logOracle() { Properties pro =new Properties(); InputStream is=null; Connection conn=null; try { //读取properties数据库配置文件(数据库名字,密码,主机名) is=new BufferedInputStream(new FileInputStream("D:\\数据库配置文件\\db.properties")); pro.load(is); String driver=pro.getProperty("driver"); String url=pro.getProperty("url"); String user=pro.getProperty("user"); String password=pro.getProperty("password"); //加载驱动 Class.forName(driver); //建立连接 conn = DriverManager.getConnection(url, user, password); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { //释放IO ReleaseIo.free(is); //释放对象资源 Releasejdbc.free(conn); } } }
释放资源
package jdbc; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /*jbac中 connection,statement,resultSet 释放资源的封装方法 * @auther:Shadow * @Date:2018.09.28 * */ public class Releasejdbc { public static void free(Connection conn, Statement st, ResultSet rs) { free(rs); free(conn, st); } public static void free(Connection conn, Statement st) { free(st); free(conn); } public static void free(Statement st, ResultSet rs) { free(rs); free(st); } public static void free(Connection conn) { try { if(conn != null && !conn.isClosed()) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void free(Statement st) { if(st != null) { try { st.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void free(ResultSet rs) { if(rs != null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
释放I/O资源
package jdbc; import java.io.Closeable; import java.io.IOException; public class ReleaseIo { public static void free(Closeable...stream){ for(Closeable st:stream){ if(st != null){ try { st.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
db.properties文件
driver=oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@localhost:1521:orcl user=TESTUSER password=123456
添加信息到数据库中
package jdbc; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Properties; //更新数据库 public class update { public static void main(String[] args) { StudentVO st=new StudentVO(); st.setSno(510); st.setName("李红"); st.setSex("女"); st.setBirthday(new Date(19961027)); st.setNumber(1401); updateOracle(st); } private static void updateOracle(StudentVO stu) { String sql="insert into awer values" + "(?,?,?,?,?)"; Properties pro =new Properties(); InputStream is=null; Connection conn=null; //PreparedStatement是statement的子类 PreparedStatement ps = null; try { is = new BufferedInputStream(new FileInputStream("D:\\数据库配置文件\\db.properties")); pro.load(is); String driver=pro.getProperty("driver"); String url=pro.getProperty("url"); String user=pro.getProperty("user"); String password=pro.getProperty("password"); Class.forName(driver); conn = DriverManager.getConnection(url, user, password); ps=conn.prepareStatement(sql); ps.setDouble(1, stu.getSno()); ps.setString(2, stu.getName()); ps.setString(3, stu.getSex()); ps.setTimestamp(4, new Timestamp(stu.getBirthday().getTime())); ps.setDouble(5, stu.getNumber()); ps.executeUpdate(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ ReleaseIo.free(is); Releasejdbc.free(conn, ps); } } }
创建一个对应数据库表格的类
package jdbc; import java.util.Date; public class StudentVO { private double sno; private String name; private String sex; private Date birthday; private double number; public double getSno() { return sno; } public void setSno(double sno) { this.sno = sno; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public double getNumber() { return number; } public void setNumber(double number) { this.number = number; } }
查询数据库
package jdbc; import oracle.jdbc.driver.OracleDriver; import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /*对数据库进行查询 * */ public class testselect { public static void main(String[] args) { select("101"); } private static void select(String no) { // 主机名和服务名 String url = "jdbc:oracle:thin:@localhost:1521:orcl"; // 数据库服务名 String user = "TESTUSER"; // 密码 String password = "123456"; String sql = "select s.sno,s.name,s.birthday from awer s " + "where s.sno='" + no + "'"; Connection conn = null; Statement st = null; ResultSet rs = null; // 1.加载驱动 try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection(url, user, password); // 3.通过数据库的连接操作数据库,实现增删改查(使用Statement类) st = conn.createStatement(); rs = st.executeQuery(sql); // 4.处理数据库的返回结果(使用ResultSet类) while (rs.next()) { String sno = rs.getString("sno"); String name = rs.getString("name"); Date birthday = rs.getDate(3); System.out.println(sno + ", " + name + ", " + birthday); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { Releasejdbc.free(conn, st, rs); } } }
对数据库进行删除
package jdbc; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /*对数据库进行删除操作 * */ public class delete { public static void main(String[] args) { daleteOracle("401"); } private static void daleteOracle(String sno) { String sql = "delete awer a where a.sno='" + sno + "';"; Properties pro = new Properties(); InputStream is = null; Connection conn = null; Statement st = null; try { is = new BufferedInputStream(new FileInputStream("D:\\数据库配置文件\\db.properties")); pro.load(is); String driver = pro.getProperty("driver"); String url = pro.getProperty("url"); String user = pro.getProperty("user"); String password = pro.getProperty("password"); Class.forName(driver); conn = DriverManager.getConnection(url, user, password); // 通过数据库的连接操作数据库,实现删除(使用Statement类) st = conn.createStatement(); st.executeUpdate(sql); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { ReleaseIo.free(is); Releasejdbc.free(conn, st); } } }