文档详情

orale数据库连接和增删查改.doc

发布:2017-11-18约6.04千字共7页下载文档
文本预览下载声明
数库连类删1.使用preparedstatement接口的对象来执行对数据库的增加数据操作 分享到 一键分享 QQ空间 新浪微博 百度搜藏 人人网 腾讯微博 百度相册 开心网 腾讯朋友 百度贴吧 豆瓣网 搜狐微博 百度新首页 QQ收藏 和讯微博 更多... 百度分享 PreparedStatement ps = conn.prepareStatement(insert into A(a,b) values(?,?)); ps.setString(0,aaaa); ps.setString(1,bbbb); ps.execute(); conn.close(); 2.DBUtils类,是一个单例模式,用来创建数据库连接,执行预处理,和关闭数据库,里面的方法都是静态的,直接用类名.方法就可以调用了。 import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtils { static { try { //加载数据库驱动 Class.forName(oracle.jdbc.OracleDriver); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //获取数据库连接对象 public static Connection getConn() { Connection conn = null; try { //jdbc:oracle:thin:@localhost:1521:名字, 用户名,密码 conn=DriverManager.getConnection(jdbc:oracle:thin:@192.168.161.3 :1521:MYDB, scott,tiger); } catch (SQLException e) { e.printStackTrace(); } return conn; } //获取语句执行对象 public static Statement getStatement(Connection conn) { Statement stmt = null; try { stmt = conn.createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return stmt; } //获取预处理语句执行对象 public static PreparedStatement getPreparedStatement(Connection conn, String sql) { PreparedStatement pstmt = null; try { pstmt = conn.prepareStatement(sql); } catch (SQLException e) { e.printStackTrace(); } return pstmt; } //获取结果集对象 public static ResultSet getResultSet(PreparedStatement pstmt) { ResultSet res = null; try { res = pstmt.executeQuery(); } catch (SQLException e) { e.printStackTrace(); } return res; } //获取结果集对象 public static ResultSet getResultSet(Statement stmt, String sql) { ResultSet res = null; try { res = stmt.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return res; } //关闭资源方法 public static void close(Connection conn, Statem
显示全部
相似文档