我正在尝试在netbeans 8.2(server-tomcat 8.0.27.0)上的Web应用程序中创建Web服务,该服务可以连接到postgres上的数据库并读取名为“ test”的表。我在download.java中有此代码(Web服务在一个名为serve的软件包中)
Download.java
undefined
这是我的context.xml文件(在META-INF中)
context.xml
@WebService(serviceName = "download")
public class Download {
Connection con=null;
private DataSource getJdbcPostgres() throws NamingException, SQLException {
Context c = new InitialContext();
DataSource ds=(DataSource) c.lookup("java:comp/env/jdbc/postgres");
con=ds.getConnection();
return ds;
}
@WebMethod(operationName = "download")
public String download(@WebParam(name = "username")String username, @WebParam(name = "id")String id) throws ClassNotFoundException, SQLException {
String sql = "select * from test where id="+id;
Class.forName("org.postgresql.Driver");
PreparedStatement pst=con.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
rs.next();
return "";
}
}
这是web.xml(在WEB-INF中):
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/service2">
<Resource name="jdbc/postgres" auth="Container"
type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/postgres"
username="someusername" password="somepassword" maxTotal="20" maxIdle="10"
maxWaitMillis="-1"/>
</Context>
clean and build命令成功,但未部署在Tomcat的Apache服务器上 错误消息(当我尝试部署它时输出):
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>download</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>download</servlet-name>
<url-pattern>/download</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<resource-ref>
<description>postgreSQL Datasource example</description>
<res-ref-name>jdbc/postgres</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
当我尝试将服务器更改为GlassFish 4.1.1时,它说:
Checking data source definitions for missing JDBC drivers...
Undeploying ...
undeploy?path=/service2
OK - Undeployed application at context path /service2
In-place deployment at D:\NetBeansProjects\service2\build\web
Deployment is in progress...
deploy?config=file%3A%2FC%3A%2FUsers%2FTRAINE%7E3%2FAppData%2FLocal%2FTemp%2Fcontext1366288511044657094.xml&path=/service2
FAIL - Deployed application at context path /service2 but context failed to start
D:\NetBeansProjects\service2\nbproject\build-impl.xml:1094: The module has not been deployed.
See the server log for details.
BUILD FAILED (total time: 2 seconds)
我是该领域的新手,所以如果我在某个地方出错或缺少某些东西,请帮助我!
答案 0 :(得分:0)
我不知道为什么会发生,但是我只是尝试捕获而不是抛出,并且它正在部署!
@WebMethod(operationName = "download")
public String download(@WebParam(name = "username")String username, @WebParam(name = "id")String id) {
try {
String sql="select * from test";
Connection conn=myDatasource.getConnection();
PreparedStatement pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
}
catch (SQLException ex) {
Logger.getLogger(connect.class.getName()).log(Level.SEVERE, null, ex);
}
return "someString";
}
任何人都可以提出建议,为什么它不能更早起作用?尽管我的问题已解决,但我很好奇知道“ throws Exception”出了什么问题! 附言这次我在GlassFish 4.1.1上运行它