Java SQL JDBC 错误 - 带有 Soap 消息
Java SQL JDBC Error -with Soap Message
我在 JDBC 中遇到问题。所有配置都是正确的。这是 class:
package eai.serviceImpt;
import java.rmi.RemoteException;
import java.sql.*;
import org.apache.axis2.AxisFault;
import eai.payCore.Payement;
import eai.serviceInvocation.Compte_managerCallbackHandler;
import eai.serviceInvocation.Compte_managerStub;
import eai.serviceInvocation.Compte_managerStub.GetCompteIdetifiant;
import eai.serviceInvocation.Compte_managerStub.GetCompteIdetifiantResponse;
public class PayementManager {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/payement";
static final String USER = "root";
static final String PASS = "";
Connection conn = null ;
PreparedStatement stmt = null;
public int manageSolde(String log , String pass , int sold ) throws
RemoteException{
Compte_managerStub stub = new Compte_managerStub
("http://localhost:8021/axis2/services/compte_manager");
GetCompteIdetifiant request = new GetCompteIdetifiant();
request.setLog(log);
request.setPass(pass);
GetCompteIdetifiantResponse response =
stub.getCompteIdetifiant(request);
Compte_managerStub.Compte compte = response.get_return();
if (compte == null)
{return -1;} //if account does not exist
else{
if (sold < 0) {return -2;} //le prix est inférieur à 0
else{
int id_payement=compte.getId_payement();
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
conn = DriverManager.getConnection(DB_URL, USER, PASS);
//STEP 4: Execute a query
String sql = "SELECT login , password , solde FROM payem
WHERE id_payement = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id_payement);
ResultSet rs = stmt.executeQuery();
//STEP 5: Extract data from result set
String login = rs.getString("login");
String password = rs.getString("password");
int solde =rs.getInt("solde");
rs.close();
//2éme verification
if (log.equals(login) && pass.equals(password)){
if ( sold > solde){
stmt.close();
conn.close();
return -4 ; //solde insuffisant
}
else{
solde = solde - sold;
//update in our database
String sql1 = "UPDATE payem SET solde = ? WHERE
id_payement = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, solde);
stmt.setInt(2, id_payement);
stmt.executeQuery();
stmt.close();
conn.close();
return solde;
}
}
else{
stmt.close();
conn.close();
return -3; //2éme verification du compte
}
}
catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
return id_payement;// 2 time to avoid try problem
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
return id_payement; // 2 time to avoid try problem
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();}
catch(SQLException se){}
try{
if(conn!=null)
conn.close();}
catch(SQLException se){
se.printStackTrace();
}
}
}
//return -5;
}
}
}
这个 class 实现了一个 soap web 服务与 manageSolde 是操作。我正在使用 SoapUI 来测试我的网络服务。我从其他 if 语句得到了很好的响应,但是当我尝试测试我有 SQL 语句的 if 语句时,程序执行 catch statement.so,我认为问题在 JDBC 或 SQL语句。
这是我的 table 在我的数据库中:
首先将代码中的 sql
更改为 sql1
如下:
//update in our database
String sql1 = "UPDATE payem SET solde = ? WHERE id_payement = ?";
stmt = conn.prepareStatement(sql1);
你必须使用 rs.next()
例如:
ResultSet rs = stmt.executeQuery();
if(rs.next()){
String login = rs.getString("login");
String password = rs.getString("password");
int solde =rs.getInt("solde");
}
其次,您在第二个语句中使用了相同的查询:
String sql1 = "UPDATE payem SET solde = ? WHERE id_payement = ?";
stmt = conn.prepareStatement(sql);
// ^^-----------------It should be sql1
我在 JDBC 中遇到问题。所有配置都是正确的。这是 class:
package eai.serviceImpt;
import java.rmi.RemoteException;
import java.sql.*;
import org.apache.axis2.AxisFault;
import eai.payCore.Payement;
import eai.serviceInvocation.Compte_managerCallbackHandler;
import eai.serviceInvocation.Compte_managerStub;
import eai.serviceInvocation.Compte_managerStub.GetCompteIdetifiant;
import eai.serviceInvocation.Compte_managerStub.GetCompteIdetifiantResponse;
public class PayementManager {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/payement";
static final String USER = "root";
static final String PASS = "";
Connection conn = null ;
PreparedStatement stmt = null;
public int manageSolde(String log , String pass , int sold ) throws
RemoteException{
Compte_managerStub stub = new Compte_managerStub
("http://localhost:8021/axis2/services/compte_manager");
GetCompteIdetifiant request = new GetCompteIdetifiant();
request.setLog(log);
request.setPass(pass);
GetCompteIdetifiantResponse response =
stub.getCompteIdetifiant(request);
Compte_managerStub.Compte compte = response.get_return();
if (compte == null)
{return -1;} //if account does not exist
else{
if (sold < 0) {return -2;} //le prix est inférieur à 0
else{
int id_payement=compte.getId_payement();
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
conn = DriverManager.getConnection(DB_URL, USER, PASS);
//STEP 4: Execute a query
String sql = "SELECT login , password , solde FROM payem
WHERE id_payement = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id_payement);
ResultSet rs = stmt.executeQuery();
//STEP 5: Extract data from result set
String login = rs.getString("login");
String password = rs.getString("password");
int solde =rs.getInt("solde");
rs.close();
//2éme verification
if (log.equals(login) && pass.equals(password)){
if ( sold > solde){
stmt.close();
conn.close();
return -4 ; //solde insuffisant
}
else{
solde = solde - sold;
//update in our database
String sql1 = "UPDATE payem SET solde = ? WHERE
id_payement = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, solde);
stmt.setInt(2, id_payement);
stmt.executeQuery();
stmt.close();
conn.close();
return solde;
}
}
else{
stmt.close();
conn.close();
return -3; //2éme verification du compte
}
}
catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
return id_payement;// 2 time to avoid try problem
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
return id_payement; // 2 time to avoid try problem
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();}
catch(SQLException se){}
try{
if(conn!=null)
conn.close();}
catch(SQLException se){
se.printStackTrace();
}
}
}
//return -5;
}
}
}
这个 class 实现了一个 soap web 服务与 manageSolde 是操作。我正在使用 SoapUI 来测试我的网络服务。我从其他 if 语句得到了很好的响应,但是当我尝试测试我有 SQL 语句的 if 语句时,程序执行 catch statement.so,我认为问题在 JDBC 或 SQL语句。
这是我的 table 在我的数据库中:
首先将代码中的 sql
更改为 sql1
如下:
//update in our database
String sql1 = "UPDATE payem SET solde = ? WHERE id_payement = ?";
stmt = conn.prepareStatement(sql1);
你必须使用 rs.next()
例如:
ResultSet rs = stmt.executeQuery();
if(rs.next()){
String login = rs.getString("login");
String password = rs.getString("password");
int solde =rs.getInt("solde");
}
其次,您在第二个语句中使用了相同的查询:
String sql1 = "UPDATE payem SET solde = ? WHERE id_payement = ?";
stmt = conn.prepareStatement(sql);
// ^^-----------------It should be sql1