Axis2 を使用して作成された Web サーバーがあります。私のクライアントは Android アプリです。Web サービスがデータベースに接続します。ただし、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;
}
}`
webserviceから呼び出すコードは
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) ) org.web.backend.calculate.Connect.Connection(Connect.java:17) で java.lang.Class.forName(未知のソース) で java.lang.Class.forName0(ネイティブ メソッド) org.web.backend で.calculate.Calculate.populateLeagues(Calculate.java:17)、sun.reflect.NativeMethodAccessorImpl.invoke0(ネイティブ メソッド)、sun.reflect.NativeMethodAccessorImpl.invoke(不明なソース)、sun.reflect.DelegatingMethodAccessorImpl.invoke(不明なソース) org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:194) の java.lang.reflect.Method.invoke(不明なソース) org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:102) org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40) 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) at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) ) org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) で org.apache.org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java: 191) org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) で org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) で org.apache.catalina.core.StandardEngineValve .invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) at org. org.apache.tomcat.util.net.JIoEndpoint$Worker の apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)。java.lang.Thread.run で実行 (JIoEndpoint.java:489) (不明なソース)
データベース サーバーに接続できません