java原生连接MySQL_java原生操作数据库

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

下载引入jdbc连接桥

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

1

2

3

4

5

6

7

com.oracle

ojdbc14

10.2.0.4.0

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

31public 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();

×

谢谢客官

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

f3399520847e351f302d24eef5260f51.png

d21bc06202198c37adebe4d0f173c717.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值