Java 数据库操作oracle增删改查,通用封装基于hashmap
pt1:首先安装oracle连接驱动 下载地址:https://pan.baidu.com/s/1jW_ofgU4eJmAn7Y2J5B46A 密码:epkz 1.将ojdbc6.jar导入项目中 先创建一个项目,然后在鼠标移到项目上右键-->new-->folder;folder name:lib;这样就在项目中创建了一个文件夹lib;然后将ojdbc6.jar包导入该文件夹中 2.Reference Library包右键-->build path-->add extend 导入jar包apply and save; package Db; import java.sql.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.HashMap; import java.util.Map; import org.openqa.selenium.remote.server.handler.DeleteCookie; import bsh.This; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class OrcTest { String url; String user; String pwd; String sql; Connection connect;// 创建一个数据库连接 PreparedStatement pre ;// 创建预编译语句对象,一般都是用这个而不用Statement ResultSet result; public OrcTest(String url,String user,String pwd){ this.url=url; this.user=user; this.pwd=pwd; try { Class.forName("oracle.jdbc.driver.OracleDriver"); this.connect=DriverManager.getConnection(this.url, this.user, this.pwd); }catch (Exception e) { System.out.println("连接数据驱动失败"); e.printStackTrace(); } } public void CloseDb(){ try { if(this.pre !=null) { this.pre.close(); System.out.println("关闭事物"); } if(this.result !=null) { this.result.close(); System.out.println("关闭结果对象"); } if(this.connect !=null) { this.connect.close(); System.out.println("关闭连接"); } }catch (Exception e) { e.printStackTrace(); } } // exe_select public ArrayList<Object> exeselect(String sql) { ArrayList<Object> list =new ArrayList<Object>(); try { this.pre=this.connect.prepareStatement(sql); this.result = this.pre.executeQuery(); ResultSetMetaData rsmd=null; int count=0; rsmd = this.result.getMetaData(); count=rsmd.getColumnCount(); while(this.result.next()) { Map<String, Object> map = new HashMap<String, Object>(); for (int i = 1; i <= count; i++) { map.put(rsmd.getColumnLabel(i),this.result.getObject(i)); } JSONObject json = JSONObject.fromObject(map); list.add(json.toString()); } }catch(Exception e) { e.printStackTrace(); }finally { this.CloseDb(); } return list; } //delete and update and insert public boolean DeleteOrUpdateInsert(String sql) { boolean bool; try { this.pre=this.connect.prepareStatement(sql); this.pre.executeUpdate(); System.out.println("操作成功!!!!!!"); bool =true; }catch (Exception e) { bool=false; e.printStackTrace(); }finally { this.CloseDb(); } return bool; } public static void main(String[] args){ String url="jdbc:oracle:thin:@localhost:1521/orcl"; String user="SCOTT"; String pwd="pipeline"; // String sql="select * from bonus"; OrcTest ob= new OrcTest(url, user, pwd); // System.out.println(ob.exeselect(sql)); // System.out.println(ob.connect); System.out.println(ob.DeleteOrUpdateInsert("insert into bonus(ENAME,JOB,SAL,COMM)VALUES('del','job_del',122,3)")); } } ============================输出如下: 操作成功!!!!!! 关闭事物 关闭连接 true ============== 关闭事物 关闭结果对象 关闭连接 [{"ENAME":"cdp","COMM":1,"JOB":"jod_1234","SAL":1254}, {"ENAME":"cdp3","COMM":2,"JOB":"job003","SAL":1333}, {"ENAME":"cdp4","COMM":2,"JOB":"job004","SAL":1444}, {"ENAME":"cdp1","COMM":2,"JOB":"job222","SAL":1235}] oracle.jdbc.driver.T4CConnection@533e64 json转换java 需要全jar包下载git地址 https://github.com/chen1932390299/pyscripts.git