0
データベースを使用してJavaでgetリクエストに応答しようとしています。しかし、それは私がそれを修正する手掛かりを知らないいくつかのエラーをスローする。データベースを使用してJavaでgetリクエストに応答します。
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
try{
//Accessing driver from the JAR file
Class.forName ("com.mysql.jdbc.Driver").newInstance();
//Connect to Clockie's database
Connection con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/database", "root", "root");
//Here we create our query
String sql = "" +
"SELECT * " +
"FROM profiles " +
"WHERE profileId = '27'";
PreparedStatement statement = con.prepareStatement(sql);
ResultSet result = statement.executeQuery();
String xmls = "";
result.next();
xmls = result.getString("firstName") + " "+result.getString("lastName");
System.out.println(xmls);
resp.getWriter().println(xmls);
}
catch(Exception e){
System.err.println(e.getMessage());
}
}
}
メインメソッド内にdoGetコードブロックを設定しても、問題はありません。私はJavaのplsヘルプの新人です!
EDIT:
例外は "はcom.mysql.jdbc.Driver" です。
クラスパスにクラスパスを追加しました。それ以外の場合は、メインメソッド内に配置するとコードブロックが機能しません。 e.printStackTrace();メソッドが無効であるため、動作していません。 'System.err.println(e.getMessage()); 'の代わりに – einstein
@ Woho87、' e.printStackTrace() 'を使って完全なエラーを教えてください。 – dogbane
にjava.lang.ClassNotFoundException:java.net.URLClassLoader $ 1.runではcom.mysql.jdbc.Driver \t(URLClassLoader.java:202) \t ..... – einstein