从 Eclipse 连接到 MySQL
我正在尝试从 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: 没有找到适合 jdbc:mysql//localhost:3306/try 异常的驱动程序。
我下载了连接器/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.
我感谢您提前提供的任何帮助:-)
I'm trying to connect to the MySql database from a Java servlet. I'm working in 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();
}
}
}
In the line
con = DriverManager.getConnection("jdbc:mysql//localhost:3306/try", "root", "password")
I get the following exception:
java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/try Exception.
I downloaded the connector/J, extracted the jar file into Tomcat library folder and set it in the class path.
I have no idea what else I could have done wrong. It also says
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.
I thank for any help in advance :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 URL 不正确。应该是
只需将驱动程序的 jar 文件放在 WEB-INF/lib 文件夹中就足够了。无需修改Tomcat安装。但是您确实应该注意关闭finally 块中的结果集、语句和连接,否则您的数据库将很快耗尽可用连接。
您还应该使用池数据源,如 Tomcat 文档中所述。
The problem is that the URL is incorrect. It should be
Just putting the jar file of the driver in your WEB-INF/lib folder should be sufficient. No need to modify the Tomcat installation. But you should really take care of closing the resultsets, statements and connections in a finally block, else your database will quickly run out of available connections.
You should also use a pooled DataSource as explained in the Tomcat documentation.