【发布时间】:2011-10-15 13:36:42
【问题描述】:
我正在尝试从 Java servlet 连接到 MySql 数据库。我在 Eclipse IDE 中工作。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql//localhost:3306/try", "root", "password");
} catch (Exception e) {
e.printStackTrace();
}
if (request.getParameter("select1") != null) {
PrintWriter out = response.getWriter();
String select1 = (String) request.getParameter("select1");
int no = Integer.parseInt(select1);
String query = "SELECT * FROM try.try where id='" + no + "'";
out.print("<option>---select one---</option>");
try {
Statement stmt = (Statement) con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
out.print("<option>" + rs.getString("output") + "</option>");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
行内
con = DriverManager.getConnection("jdbc:mysql//localhost:3306/try", "root", "password")
我得到以下异常:
java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/try Exception.
我下载了连接器/J,将 jar 文件解压缩到 Tomcat 库文件夹中,并将其设置在类路径中。
我不知道我还能做错什么。它还说
The JAR file C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\mysql-connector-java-5.1.18-bin.jar has no source attachment.
提前感谢您的帮助:-)
【问题讨论】:
标签: java mysql servlets connection