首页上一页 1 下一页尾页 1 条记录 1/1页
评论下我的代码;在事务安全上是不是有问题,如何改好呢?
发表在Java图书答疑
2010-02-26
是否精华
是
否
版块置顶:
是
否
package baobao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JTextField;
public class Shu_ju {
String s1;
String s2;
String s3;
String s4;
String s5;
String s6;
String s7;
String s8;
Connection con = null;
PreparedStatement sql_1 = null;
PreparedStatement sql_2 = null;
public Shu_ju() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("驱动加载 ok");
con = DriverManager
.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=my_db",
"sa", "liuziyi");
System.out.println("数据库连接 ok");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败");
}
}
public void insert_db(String s1, String s2, String s3, String s4,
String s5, String s6, String s7, String s8)
{
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
this.s4 = s4;
this.s5 = s5;
this.s6 = s6;
this.s7 = s7;
this.s8 = s8;
try {
sql_1 = con
.prepareStatement("insert into rukudan_mingxi_biao values(?,?,?,?)");
sql_1.setString(1, s1);
sql_1.setString(2, s2);
sql_1.setString(3, s3);
sql_1.setString(4, s4);
sql_1.executeUpdate();
sql_2 = con
.prepareStatement("insert into shangping_mingxi_biao values(?,?,?,?)");
sql_2.setString(1, s5);
sql_2.setString(2, s6);
sql_2.setString(3, s7);
sql_2.setString(4, s8);
sql_2.executeUpdate();
}
catch (SQLException e)
{
try {con.rollback();}
catch (SQLException e1) {e1.printStackTrace();}
}
finally {
try {con.close();}
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;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JTextField;
public class Shu_ju {
String s1;
String s2;
String s3;
String s4;
String s5;
String s6;
String s7;
String s8;
Connection con = null;
PreparedStatement sql_1 = null;
PreparedStatement sql_2 = null;
public Shu_ju() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("驱动加载 ok");
con = DriverManager
.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=my_db",
"sa", "liuziyi");
System.out.println("数据库连接 ok");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败");
}
}
public void insert_db(String s1, String s2, String s3, String s4,
String s5, String s6, String s7, String s8)
{
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
this.s4 = s4;
this.s5 = s5;
this.s6 = s6;
this.s7 = s7;
this.s8 = s8;
try {
sql_1 = con
.prepareStatement("insert into rukudan_mingxi_biao values(?,?,?,?)");
sql_1.setString(1, s1);
sql_1.setString(2, s2);
sql_1.setString(3, s3);
sql_1.setString(4, s4);
sql_1.executeUpdate();
sql_2 = con
.prepareStatement("insert into shangping_mingxi_biao values(?,?,?,?)");
sql_2.setString(1, s5);
sql_2.setString(2, s6);
sql_2.setString(3, s7);
sql_2.setString(4, s8);
sql_2.executeUpdate();
}
catch (SQLException e)
{
try {con.rollback();}
catch (SQLException e1) {e1.printStackTrace();}
}
finally {
try {con.close();}
catch (SQLException e) {e.printStackTrace();}
}
}
}