Oracle高级应用+jdbc

package com;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class T1 {
public Connection getConnection() {
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:ora", "scott", "tiger");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}

public void sql1(String name1, String name2, int money) {
Connection conn = null;

PreparedStatement pre = null;
try {
conn = getConnection();

pre = conn
.prepareStatement("update zhangdan set money=money+100 where name='tom'");
pre.execute();
pre = conn
.prepareStatement("update zhangdan set money=money-100 where name='marry'");
pre.execute();
System.out.println("22wwwwwwwwww2");
conn.setAutoCommit(false);
conn.commit();
System.out.println("操作无误提交完成");

} catch (SQLException e) {
try {
conn.rollback();
System.out.println("error,返回");
} catch (SQLException e1) {
e1.printStackTrace();
}
// e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
System.out.println("conn流已经关闭");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (pre != null) {
try {
pre.close();
System.out.println("pre流已经关闭");

} catch (SQLException e) {

e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
T1 t = new T1();
String name1 = "tom";
String name2 = "marry";
int money = 1000;
t.sql1(name1, name2, money);

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值