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;packageDb;
importjava.sql.*;
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.List;
importjava.util.HashMap;
importjava.util.Map;
importorg.openqa.selenium.remote.server.handler.DeleteCookie;
importbsh.This;
import.NET.sf.JSON.JSONArray;
import.NET.sf.JSON.JSONObject;
publicclassOrcTest{
Stringurl;
Stringuser;
Stringpwd;
Stringsql;
Connectionconnect;// 创建一个数据库连接
PreparedStatementpre;// 创建预编译语句对象, 一般都是用这个而不用 Statement
ResultSetresult;
publicOrcTest(Stringurl,Stringuser,Stringpwd){
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(Exceptione){
System.out.println("连接数据驱动失败");
e.printStackTrace();
}
}
publicvoidCloseDb(){
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(Exceptione){
e.printStackTrace();
}
}
// exe_select
publicArrayListexeselect(Stringsql){
ArrayListlist=newArrayList();
try{
this.pre=this.connect.prepareStatement(sql);
this.result=this.pre.executeQuery();
ResultSetMetaDatarsmd=null;
intcount=0;
rsmd=this.result.getMetaData();
count=rsmd.getColumnCount();
while(this.result.next()){
Mapmap=newHashMap();
for(inti=1;i<=count;i++){
map.put(rsmd.getColumnLabel(i),this.result.getObject(i));
}
JSONObjectJSON=JSONObject.fromObject(map);
list.add(JSON.toString());
}
}catch(Exceptione){
e.printStackTrace();
}finally{
this.CloseDb();
}
returnlist;
}
//delete and update and insert
publicbooleanDeleteOrUpdateInsert(Stringsql){
booleanbool;
try{
this.pre=this.connect.prepareStatement(sql);
this.pre.executeUpdate();
System.out.println("操作成功!!!!!!");
bool=true;
}catch(Exceptione){
bool=false;
e.printStackTrace();
}finally{
this.CloseDb();
}
returnbool;
}
publicstaticvoidmain(String[]args){
Stringurl="jdbc:oracle:thin:@localhost:1521/orcl";
Stringuser="SCOTT";
Stringpwd="pipeline";
// String sql="select * from bonus";
OrcTestob=newOrcTest(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
来源: http://www.bubuko.com/infodetail-2988874.html