我有一个使用 Axis2 创建的网络服务器。我的客户是安卓应用。Web 服务连接到数据库。但网络服务会自动停止。
连接数据库的代码`public class Connect { public Connection Connection() {
Connection conn = null;
try {
String userName = "Admin";
String password = "admin";
//String url = "jdbc:mysql://mysql.cs.nott.ac.uk:3306/svp01m";
String url = "jdbc:mysql://localhost/tests";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, userName, password);
System.out.println ("\n Database connection established");
} catch (Exception e) {
e.printStackTrace();
System.err.println("\n Cannot connect to database server");
System.exit(0);
}
return conn;
}
}`
从 web 服务调用的代码是
public class Calculate
{
public boolean populateLeagues(String username, String password){
try {
/*Create object of the Connect class*/
Connect connect = new Connect();
/* calling the Connection method of Connect class to establish a database connection*/
Connection conn = connect.Connection();
java.sql.PreparedStatement preparedStatement = null;
/* Creating a query to be executed with prepared statement*/
String query = "select name from users where name=?";
preparedStatement = conn.prepareStatement(query);
/*substitutes the value for ? in the prepared Statement */
preparedStatement.setString(1, username);
/* Result set to capture the results of the query*/
ResultSet rs = preparedStatement.executeQuery();
boolean empty = true;
while (rs.next()) {
empty=false;
System.err.println("\n User Name exists \n");
return true;
}
if(empty)
{
System.out.println("\n the userame does not exists");
return false;
}
rs.close();
conn.close();
} catch (Exception e) {
//e.printStackTrace();
System.err.println ("Cannot connect to database server");
System.exit(0);
}
return true;
}
我得到的例外是
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526 ) 在 java.lang.Class.forName0(Native Method) 在 java.lang.Class.forName(Unknown Source) 在 org.web.backend.calculate.Connect.Connection(Connect.java:17) 在 org.web.backend .calculate.Calculate.populateLeagues(Calculate.java:17) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 在java.lang.reflect.Method.invoke(Unknown Source) at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:194) at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:102) at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40) at org.apache.axis2.receivers.AbstractMessageReceiver.receive( AbstractMessageReceiver.java:100) 在 org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:176) 在 org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:275) 在 org.apache .axis2.transport.http.AxisServlet.doPost(AxisServlet.java:133) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:717 ) 在 org.apache 的 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)。catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java: 191) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 在 org.apache.catalina.core.StandardEngineValve 的 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) .invoke(StandardEngineValve.java:109) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 在 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) 在 org. org.apache.tomcat.util.net.JIoEndpoint$Worker 上的 apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)。在 java.lang.Thread.run 运行(JioEndpoint.java:489)(未知来源)
无法连接到数据库服务器