从 Eclipse 连接到 MySQL

发布于 2024-12-10 05:34:02 字数 1649 浏览 2 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不离久伴 2024-12-17 05:34:02

问题是 URL 不正确。应该是

jdbc:mysql://localhost:3306/try
          ^-- you missed the colon here

只需将驱动程序的 jar 文件放在 WEB-INF/lib 文件夹中就足够了。无需修改Tomcat安装。但是您确实应该注意关闭finally 块中的结果集、语句和连接,否则您的数据库将很快耗尽可用连接。

您还应该使用池数据源,如 Tomcat 文档中所述

The problem is that the URL is incorrect. It should be

jdbc:mysql://localhost:3306/try
          ^-- you missed the colon here

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文