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
来源: http://www.bubuko.com/infodetail-2988874.html