当前位置: 技术问答>java相关
那位帮我解决一下,100分相送,谢谢
来源: 互联网 发布时间:2015-11-14
本文导语: import java.sql.*; import db.*; public class TransactionTest { /** DATABASE QUERIES **/ private static final String INSERT_DEPART = "INSERT INTO f_depart(departId, name) VALUES(?,?)"; private static final String IN...
import java.sql.*;
import db.*;
public class TransactionTest {
/** DATABASE QUERIES **/
private static final String INSERT_DEPART =
"INSERT INTO f_depart(departId, name) VALUES(?,?)";
private static final String INSERT_PRODUCT =
"INSERT INTO f_depart(name) VALUES(?)";
public TransactionTest() {
}
public void insertDepart(Connection conn){
PreparedStatement pstmt = null;
try{
pstmt = conn.prepareCall(INSERT_DEPART);
pstmt.setInt(1,554);
pstmt.setString(2,"union");
pstmt.executeUpdate();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
public void insertProduct(Connection conn){
PreparedStatement pstmt = null;
try{
pstmt = conn.prepareCall(INSERT_PRODUCT);
pstmt.setString(1,"union");
pstmt.executeUpdate();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
TransactionTest transactionTest1 = new TransactionTest();
try{
Connection conn = SQLDBConn.getInstance();
conn.setAutoCommit(false);
transactionTest1.insertDepart(conn);
transactionTest1.insertProduct(conn);
conn.commit();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
系统提示:
[Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.
import db.*;
public class TransactionTest {
/** DATABASE QUERIES **/
private static final String INSERT_DEPART =
"INSERT INTO f_depart(departId, name) VALUES(?,?)";
private static final String INSERT_PRODUCT =
"INSERT INTO f_depart(name) VALUES(?)";
public TransactionTest() {
}
public void insertDepart(Connection conn){
PreparedStatement pstmt = null;
try{
pstmt = conn.prepareCall(INSERT_DEPART);
pstmt.setInt(1,554);
pstmt.setString(2,"union");
pstmt.executeUpdate();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
public void insertProduct(Connection conn){
PreparedStatement pstmt = null;
try{
pstmt = conn.prepareCall(INSERT_PRODUCT);
pstmt.setString(1,"union");
pstmt.executeUpdate();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
TransactionTest transactionTest1 = new TransactionTest();
try{
Connection conn = SQLDBConn.getInstance();
conn.setAutoCommit(false);
transactionTest1.insertDepart(conn);
transactionTest1.insertProduct(conn);
conn.commit();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}
系统提示:
[Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.
|
http://expert.csdn.net/Expert/topic/1198/1198817.xml?temp=.5247309
|
//应可以
import java.sql.*;
import db.*;
public class TransactionTest {
/** DATABASE QUERIES **/
private static final String INSERT_DEPART =
"INSERT INTO f_depart(departId, name) VALUES(544,"union")";
private static final String INSERT_PRODUCT =
"INSERT INTO f_depart(name) VALUES("union")";
public TransactionTest() {
}
public void insertDepart(Connection conn,String sql){
Statement pstmt = null;
try{
pstmt = conn.createstatement(sql);
pstmt.execute();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
TransactionTest transactionTest1 = new TransactionTest();
try{
Connection conn = SQLDBConn.getInstance();
conn.setAutoCommit(false);
transactionTest1.insertDepart(conn,INSERT_DEPART);
transactionTest1.insertDepart(conn,INSERT_PRODUCT);
conn.commit();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
finally{if (conn!=null)conn.close();}
}
}
import java.sql.*;
import db.*;
public class TransactionTest {
/** DATABASE QUERIES **/
private static final String INSERT_DEPART =
"INSERT INTO f_depart(departId, name) VALUES(544,"union")";
private static final String INSERT_PRODUCT =
"INSERT INTO f_depart(name) VALUES("union")";
public TransactionTest() {
}
public void insertDepart(Connection conn,String sql){
Statement pstmt = null;
try{
pstmt = conn.createstatement(sql);
pstmt.execute();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
TransactionTest transactionTest1 = new TransactionTest();
try{
Connection conn = SQLDBConn.getInstance();
conn.setAutoCommit(false);
transactionTest1.insertDepart(conn,INSERT_DEPART);
transactionTest1.insertDepart(conn,INSERT_PRODUCT);
conn.commit();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
finally{if (conn!=null)conn.close();}
}
}
|
//这样呢?
import java.sql.*;
import db.*;
public class TransactionTest {
/** DATABASE QUERIES **/
private static final String INSERT_DEPART =
"INSERT INTO f_depart(departId, name) VALUES(544,"union")";
private static final String INSERT_PRODUCT =
"INSERT INTO f_depart(name) VALUES("union")";
public Connection =null;
public TransactionTest() {
conn = SQLDBConn.getInstance();
conn.setAutoCommit(false);
}
public void insertDepart(String sql){
Statement pstmt = null;
try{
pstmt = conn.createStatement(sql);
pstmt.execute();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
TransactionTest transactionTest1 = new TransactionTest();
try{
transactionTest1.insertDepart(INSERT_DEPART);
transactionTest1.insertDepart(INSERT_PRODUCT);
conn.commit();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
finally{if (conn!=null)conn.close();}
}
}
import java.sql.*;
import db.*;
public class TransactionTest {
/** DATABASE QUERIES **/
private static final String INSERT_DEPART =
"INSERT INTO f_depart(departId, name) VALUES(544,"union")";
private static final String INSERT_PRODUCT =
"INSERT INTO f_depart(name) VALUES("union")";
public Connection =null;
public TransactionTest() {
conn = SQLDBConn.getInstance();
conn.setAutoCommit(false);
}
public void insertDepart(String sql){
Statement pstmt = null;
try{
pstmt = conn.createStatement(sql);
pstmt.execute();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
TransactionTest transactionTest1 = new TransactionTest();
try{
transactionTest1.insertDepart(INSERT_DEPART);
transactionTest1.insertDepart(INSERT_PRODUCT);
conn.commit();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
finally{if (conn!=null)conn.close();}
}
}
|
不用开发工具带的驱动程序试试,在Connection conn = SQLDBConn.getInstance();前加载SQL2000的驱动程序试试
|
conn.setAutoCommit(false);是设置不自动提交,手工提交
在手工提交的模式下,不能同时用两个复制的connection
在手工提交的模式下,不能同时用两个复制的connection
|
如果不涉及到clob,blob等大文本对象操作的话,可以去掉
conn.setAutoCommit(false);这句话
conn.setAutoCommit(false);这句话
|
已经给你url,好好看看吧!