public class OracleDemo05 {
public static void main(String[] args)throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("name:");
String name = br.readLine();
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@x1.zongxuan.online:1521:xx";
String user = "scott";
String pass = "xxxxx";
Connection con = DriverManager.getConnection(url,user,pass);
String sql = "select empno,ename,sal from emp2 where ename=?";
PreparedStatement stmt = con.prepareStatement(sql);
//将 sql 中 第一个问号的值设置为字符串
stmt.setString(1,name);
ResultSet rs = stmt.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1)+","+rs.getString(2)+","+rs.getInt(3));
}
con.close();
}
}
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class OracleDemo06 {
public static void main(String[] args) throws Exception{
Class.forName("oracle.jdbc.OracleDriver");
String url ="jdbc:oracle:thin:@x1.zongxuan.online:1521:xx";
String user ="scott";
String pass = "xxxxx";
Connection con = DriverManager.getConnection(url,user,pass);
String sql ="insert into emp2(empno,ename) values(?,?)";
PreparedStatement stmt = con.prepareStatement(sql);
int n =0;
for(int i =1000;i 使用con.setAutoCommit(false)
package jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class OracleDemo12 {
public static void main(String[] args) throws Exception{
Connection con =null;
try{
con = DBUtils.openConnection();
con.setAutoCommit(false);
PreparedStatement stmt = con.prepareStatement("update pay set money = money-500 where id=10");
int n = stmt.executeUpdate();
System.out.println(n+"行"+"id = 10 money -500");
if (true) {
throw new Exception("模仿异常");
}
PreparedStatement stmt2 = con.prepareStatement("update pay set money = money+500 where id=100");
stmt2.executeUpdate();
System.out.println("id = 100 money +500");
con.commit();
}catch(Exception e){
con.rollback();
e.printStackTrace();
}
finally{
if(con!=null){
con.close();
}
}
}
}
package jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class OracleDemo12 {
public static void main(String[] args) throws Exception{
Connection con =null;
try{
con = DBUtils.openConnection();
con.setAutoCommit(false);
PreparedStatement stmt = con.prepareStatement("update pay set money = money-500 where id=10");
int n = stmt.executeUpdate();
System.out.println(n+"行"+"id = 10 money -500");
PreparedStatement stmt2 = con.prepareStatement("update pay set money = money+500 where id=100");
stmt2.executeUpdate();
System.out.println("id = 100 money +500");
con.commit();
}catch(Exception e){
con.rollback();
e.printStackTrace();
}
finally{
if(con!=null){
con.close();
}
}
public class OracleDemo13 {
public static void main(String[] args) throws Exception{
Connection con = DBUtils2.getConnection();
String sql = "insert into emp100(id,name) values(?,?)";
PreparedStatement stmt = con.prepareStatement(sql);
//批量插入
for(int i =1;i