无法从java应用程序连接到sqlserver

时间:2014-12-26 16:25:52

标签: java sql-server netbeans

我有Web服务,需要连接到sqlserver来进行登录,但不知何故它不起作用,它会引发异常。我使用以下步骤进行连接:

来自服务客户:

 TrackingWS_Service service=new TrackingWS_Service();
 User user = service.getTrackingWSPort().login(username,password);

TrackingWS_Service,它是来自同一项目的生成类:

@WebServiceClient(name = "TrackingWS", targetNamespace = "http://webservices/", wsdlLocation =  "http://localhost:8080/TrackingSystem/TrackingWS?wsdl")
 public class TrackingWS_Service extends Service
 {
    @WebEndpoint(name = "TrackingWSPort")
    public TrackingWS getTrackingWSPort() {
    return super.getPort(new QName("http://webservices/", "TrackingWSPort"), TrackingWS.class);
    }
 }

界面TrackingWs:

 @WebService(name = "TrackingWS", targetNamespace = "http://webservices/")
 @XmlSeeAlso({
 ObjectFactory.class
 })
 public interface TrackingWS {

    @WebMethod
    @WebResult(targetNamespace = "")
    @RequestWrapper(localName = "login", targetNamespace = "http://webservices/", className = "webservices.Login")
    @ResponseWrapper(localName = "loginResponse", targetNamespace = "http://webservices/", className = "webservices.LoginResponse")
    @Action(input = "http://webservices/TrackingWS/loginRequest", output = "http://webservices/TrackingWS/loginResponse")
     public User login(
    @WebParam(name = "arg0", targetNamespace = "")
    String arg0,
    @WebParam(name = "arg1", targetNamespace = "")
    String arg1);
 }

在服务项目中我有以下内容:

TrackingWs - 网络服务:

 @WebService(serviceName = "TrackingWS")
 public class TrackingWS {
   @WebMethod
   public User login(String username,String password){
   DBOperations dbOperations=new DBOperations();
   return dbOperations.findUser(username, password);     
   }
 }

DBOperations:

public class DBOperations {
private Connection connection = null;
private ResultSet rs = null;
private java.sql.PreparedStatement pst = null;

public synchronized User findUser(String username,String password){
connection=Connect.ConnectDb();

String sql="SELECT * FROM [Assignment5].[dbo].[User] WHERE Username=? AND Password=?";
        User user=null;
        int id;
        String usrname;
        String pasword;
        String type;
    try{
        pst=connection.prepareStatement(sql);
        pst.setString(1, username);
        pst.setString(2, password);
        rs=pst.executeQuery();
        if(rs.next()){
                    id=rs.getInt("ID");
                    usrname=rs.getString("Username");
                    pasword=rs.getString("Password");
                type=rs.getString("Type");    
                user=new User(id,usrname,pasword,type);
                    close();
                    return user;
            }

    }
    catch(SQLException e){
        e.printStackTrace();

    }
    try {
        close();
    } catch (SQLException ex) {
        Logger.getLogger(DBOperations.class.getName()).log(Level.SEVERE, null, ex);
    }
  return user;
}

}

最后连接:

public class Connect {    //连接conn = null;

public static Connection ConnectDb(){

    try {
    Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=Assignment5;integratedSecurity=true;");  
    return conn;
}
catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
                    return null;
}
} 

}

我设置了sqljdbc4,我还复制了Web服务项目中的sqljdbc_auth.dll。我不知道它为什么在调用getConnection()时抛出异常。

2 个答案:

答案 0 :(得分:1)

将SQLServer与java连接时获取异常的可能原因

  1. 未正确设置类路径时。
  2. 在您正在使用的服务器的context.xml文件中添加数据库的资源 像这样
  3. <Resource name="jdbc/vt"
    
    auth="Container"
    
    type="javax.sql.DataSource"
    
    username="ankit"
    
    password="ankit"
    
    driverClassName="com.mysql.jdbc.Driver"
    
    url="jdbc:sqlserver://localhost\\SQLEXPRESS;Database=vt"
    
    maxActive="15"
    
    maxIdle="3"/>
    
    1. 添加资源后,在您的应用程序web.xml文件中设置对该资源的引用,如此
    2. <resource-ref>
          <description>Connection Pool</description>
          <res-ref-name>jdbc/vt</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
      </resource-ref>
      

      注意:资源根据我的项目,相应地更改为您的。

      希望有所帮助

答案 1 :(得分:0)

最后我让它工作了,我只需要将jdbc jar添加到glassfish-4.1 \ glassfish \ domains \ domain1 \ lib文件夹