简体   繁体   中英

calling SQL by webservice, but it doesn't work

I got a problem with my codes.

I wrote a simple java application connected to postgreSQL, code at below, it's work.

package jdbctest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JDBCtest 
{
    public static void main(String[] args) 
    {
        Connection con = null;
        PreparedStatement pst = null;

        String url = "jdbc:postgresql://localhost/postgres";
        String user = "root";
        String password = "1234";
        String SQL="";

        try 
        {
            String a="pswd";
            con = DriverManager.getConnection(url, user, password);

            //Insert            
            SQL = "Insert into inserttable(idnb,data)  Values('ID',?)";
            pst = con.prepareStatement(SQL);
            pst.setString(1,a);
            pst.executeUpdate();
        } 
        catch (SQLException ex) 
        {
            Logger lgr = Logger.getLogger(JDBCtest .class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } 
        finally 
        {
            try 
            {

                if(con != null) con.close();
                if(pst!=null) pst.close();
            } 
            catch (SQLException ex) 
            {
                Logger lgr = Logger.getLogger(JDBCtest .class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }    
    }
}

then I created a webservice to do same thing, WSDL created without error.

package Use.SQL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.Oneway;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

@WebService(serviceName = "pgSQL")
public class pgSQL 
{
    @WebMethod(operationName = "insert")
    @Oneway
    public void insert(@WebParam(name = "infor") String infor) 
    {

        Connection con = null;
        PreparedStatement pst = null;
        ResultSet Check = null;

        String url = "jdbc:postgresql://localhost/postgres";
        String user = "root";
        String password = "1234";
        String SQL="";
        try 
        {
            con = DriverManager.getConnection(url, user, password);
            //Insert            
            SQL = "Insert into inserttable(idnb,data)  Values('ID',?)";
            pst = con.prepareStatement(SQL);
            pst.setString(1,infor);
            pst.executeUpdate();
        } 
        catch (SQLException ex) 
        {
            Logger lgr = Logger.getLogger(pgSQL .class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } 
        finally 
        {
            try 
            {
                if (con != null) con.close();
                if(pst!=null) pst.close();
            } 
            catch (SQLException ex) 
            {
                Logger lgr = Logger.getLogger(pgSQL .class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }    
    }
 }

after all, I wrote an application calling wsdl like below.

public class JavaApplication 
{
    public static void main(String[] args) 
    {
        insert("pswd");
    }    

    private static void insert(java.lang.String infor) {
        use.sql.PgSQL_Service service = new use.sql.PgSQL_Service();
        use.sql.PgSQL port = service.getPgSQLPort();
        port.insert(infor);
    }
}

Running project done without error, but nothing happend in database.

guys, have any idea about the problem?


Thanks guys, you really gave me useful messages.

now I realized the problem cause by the driver.

I surely added the jar file in project, but don't know how, webservice can't get it.

still not solve yet.


It finally works after import Driver by code.

thanks guys :D

String url = "jdbc:postgresql://localhost/postgres";

如果您的PostgreSQL端口不是80,请写入端口

String url = "jdbc:postgresql://localhost:port/postgres";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM