java原生操作数据库

使用java代码操作oracle数据库需要下载oracle的jdbc连接桥,使用其连接操作。

下载引入jdbc连接桥

在项目中我使用的是maven构建的项目,由于oracle授权的关系maven源中不可以直接下载。需要到官方的网站去下载,然后手动添加到maven本地仓库里边。

1
2
3
4
5
6
7
<!-- 添加oracle jdbc driver -->  
<!-- https://mvnrepository.com/artifact/com.oracle/ojdbc14 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>

oracle连接

连接配置

1
2
3
4
5
6
{
"oracle.driver":"oracle.jdbc.driver.OracleDriver",
"oracle.url":"jdbc:oracle:thin:@192.168.1.5:1521:orcl",
"oracle.user":"admin",
"oracle.password":"123456"
}

oracle连接类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class OracleCon {
public static Connection getConn(){
//声明Connection对象
Connection con = null;
//遍历查询结果集
try {
//加载驱动程序
JSONObject conf = Configure.getApiConf();
Class.forName(conf.optString("driver"));
//1.getConnection()方法,连接MySQL数据库!!
con = DriverManager.getConnection(conf.optString("url"),conf.optString("user"),conf.optString("password"));
if(!con.isClosed()){
//System.out.println("目标数据库连接成功!");
}
} catch(ClassNotFoundException e) {
//数据库驱动类异常处理
System.out.println("Sorry,can`t find the Driver! 找不到数据库连接驱动! ");
e.printStackTrace();
} catch(SQLException e) {
//数据库连接失败异常处理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();

}finally{
//System.out.println("数据连接结束!!");
}
return con;
}
}

入库操作

StringBuffer sql = new StringBuffer();
sql.append( " INSERT INTO   LETOUR.WEBVIEWLOGS  (\"querycode\",\"originaddress\","
        + " \"desaddress\",\"url\",\"agentos\",\"agentosname\","
        + "\"agentuaname\",\"agentdevicetype\",\"viewdatetime\" ) "
        + " VALUES(?,?,?,?,?,?,?,?,?) ");
Connection con = OracleCon.getConn();

datetime = datetime.replace("T", " ");
datetime = datetime.replace("+08:00", " ");
// 关闭事务自动提交
con.setAutoCommit(false);
PreparedStatement pst = (PreparedStatement) con.prepareStatement(sql.toString());
pst.setString(1, topic);
pst.setString(2, oip);
pst.setString(3, dip);
pst.setString(4, Url);
pst.setString(5, userAgentInfo.getOsFamily());
pst.setString(6, userAgentInfo.getOsName());
pst.setString(7, userAgentInfo.getUaName());
pst.setString(8, userAgentInfo.getDeviceType());
pst.setString(9,datetime);
pst.addBatch();
// 执行批量更新
pst.executeBatch();
// 语句执行完毕,提交本事务
con.commit();
pst.close();
con.close();

×

谢谢客官

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. 下载引入jdbc连接桥
  2. 2. oracle连接
    1. 2.1. 连接配置
    2. 2.2. oracle连接类
  3. 3. 入库操作
,