更新oracle数据库不成功,语句都执行了,但是数据库没有改变,还是原来的样子 5
packagecom.test1;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sq...
package com.test1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class UpdateRecord {
public static void main(String[] args) {
String JDriver="oracle.jdbc.OracleDriver";
try {
Class.forName(JDriver);
} catch (ClassNotFoundException e) {
System.out.println("无法加载JDBC驱动器。"+e.getMessage());
}
Connection con=null;
Statement s=null;
PreparedStatement ps=null;
try {
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","SYSTEM","sa");
String[] id={"00002","00003"};
int[] totalmoney={783,1231};
ps=con.prepareStatement("update consumer set totalmoney=?"+"where id=?");
int i=0;
do{
ps.setInt(1, totalmoney[i]);
ps.setString(2, id[i]);
ps.executeUpdate();
i++;
}while(i<id.length);
System.out.println("更新成功");
s=con.createStatement();
ResultSet rs=s.executeQuery("select * from consumer");
System.out.println("id\t\tname\t\ttotalmoney");
while(rs.next()){
System.out.println(rs.getString("id")+"\t"+rs.getString("name")+"\t\t"+rs.getString("totalmoney"));
}
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
} catch (SQLException e) {
System.out.println("更新失败"+e.getMessage());
}finally{
try {
if(ps!=null){
ps.close();
ps=null;
}
if(s!=null){
s.close();
s=null;
}
if(con!=null){
con.close();
con=null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} 展开
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class UpdateRecord {
public static void main(String[] args) {
String JDriver="oracle.jdbc.OracleDriver";
try {
Class.forName(JDriver);
} catch (ClassNotFoundException e) {
System.out.println("无法加载JDBC驱动器。"+e.getMessage());
}
Connection con=null;
Statement s=null;
PreparedStatement ps=null;
try {
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","SYSTEM","sa");
String[] id={"00002","00003"};
int[] totalmoney={783,1231};
ps=con.prepareStatement("update consumer set totalmoney=?"+"where id=?");
int i=0;
do{
ps.setInt(1, totalmoney[i]);
ps.setString(2, id[i]);
ps.executeUpdate();
i++;
}while(i<id.length);
System.out.println("更新成功");
s=con.createStatement();
ResultSet rs=s.executeQuery("select * from consumer");
System.out.println("id\t\tname\t\ttotalmoney");
while(rs.next()){
System.out.println(rs.getString("id")+"\t"+rs.getString("name")+"\t\t"+rs.getString("totalmoney"));
}
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
} catch (SQLException e) {
System.out.println("更新失败"+e.getMessage());
}finally{
try {
if(ps!=null){
ps.close();
ps=null;
}
if(s!=null){
s.close();
s=null;
}
if(con!=null){
con.close();
con=null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询