Java JDBC 连接 MySQL

发布于 2024-09-01 01:36:40 字数 1003 浏览 1 评论 0原文

我正在尝试从 Eclipse 中的 Java Web 应用程序连接到 mysql。

 Connection con = null; 

  try {
    //DriverManager.registerDriver(new com.mysql.jdbc.Driver());
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost/db_name","root" ,"");

    if(!con.isClosed())
      System.out.println("Successfully connected to " +
        "MySQL server using TCP/IP...");

  } catch(Exception e) {
    System.err.println("Exception: " + e.getMessage());
  } finally {
    try {
      if(con != null)
        con.close();
    } catch(SQLException e) {
     System.out.println(e.toString());
    }
  }

我总是遇到异常: com.mysql.jdbc.Driver

我已经下载了这个 jar http://forums.mysql.com/read.php?39,218287,220327

导入到“java build path/lib”

下mysql版本为5.1.3。

正在运行:

mysql 5.1.3(数据库已启动并正在运行来自 PHP 的查询)

Windows XP

Java EE

I am trying to connect to mysql from java web application in eclipse.

 Connection con = null; 

  try {
    //DriverManager.registerDriver(new com.mysql.jdbc.Driver());
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost/db_name","root" ,"");

    if(!con.isClosed())
      System.out.println("Successfully connected to " +
        "MySQL server using TCP/IP...");

  } catch(Exception e) {
    System.err.println("Exception: " + e.getMessage());
  } finally {
    try {
      if(con != null)
        con.close();
    } catch(SQLException e) {
     System.out.println(e.toString());
    }
  }

I am always getting the Exception: com.mysql.jdbc.Driver

I have downloaded this jar http://forums.mysql.com/read.php?39,218287,220327

import it to the "java build path/lib"

the mysql version is 5.1.3 under.

running:

mysql 5.1.3 (db is up and running queries form PHP)

windows XP

Java EE

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

混吃等死 2024-09-08 01:36:40

你说你把JAR放在构建路径上,它是在运行时类路径中吗?你说“java EE”,所以我假设你正在将其部署为网络应用程序?在这种情况下,您需要将 JDBC JAR 文件放入 Web 应用程序的 WEB-INF/lib 中。

you say you placed the JAR on the build path, is it in the runtime class path? you said "java EE", so i assume you are deploying this as a web app? in that case you need to put the JDBC JAR file into WEB-INF/lib of your web app.

我的奇迹 2024-09-08 01:36:40

您必须下载 mysql jdbc 连接器并将其用作 lib。您可以在这里获取它:

http://www.mysql.com/downloads/connector/j /

You have to download the mysql jdbc connector und use it as lib. You can get it here:

http://www.mysql.com/downloads/connector/j/

叫嚣ゝ 2024-09-08 01:36:40

您是否也将其添加到构建配置中,如果该位置不在类路径中,那么仅将其保留在 lib 文件夹中将无法工作。
检查你的 .classpath 文件

Have u added it in your build configuration also, if the location is not in classpath then it wont work by mere keeping it in lib folder.
check your .classpath file

眼眸 2024-09-08 01:36:40

如果您下载 MySQL JDBC 驱动程序
完成配置后,您必须通过获取服务器连接资源来从服务器获取连接。
以下是如何从 Glassfish 服务器获取连接资源的示例代码:

import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


@Stateless
public class AppConnection {
    private InitialContext context;
    private DataSource source;
    private Connection connection;   

    public AppConnection() {
        this.initConnection();
    }

    private void initConnection() {
        try {
            context = new InitialContext();            
            source = (DataSource)context.lookup("jdbc/MySQLDataSource");
            connection = source.getConnection();
        } catch (SQLException ex) {
            Logger.getLogger(AppConnection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(AppConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }        

    public Connection getContextConnection() {
        return connection;
    }

    public void closeContextConnection() throws SQLException {
        if(connection != null) {
            connection.close();
        }
    }
} 

请注意,此类是 EJB,“jdbc/MySQLDataSource”是服务器上配置的连接的名称。它可以注入其他 EJB 中以获取当前连接并创建其他所需的对象作为 StatementsResultsets

You can find a full documentation (Chapter 14) on how to connect to MySQL database through any web application (JBOSS, Glassfish etc...), if you download the MySQL JDBC driver.
After doing your configuration, you have to get the connection from your server by getting your server connection ressource.
Here is a sample code of how to get a connection ressource from a Glassfish server :

import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


@Stateless
public class AppConnection {
    private InitialContext context;
    private DataSource source;
    private Connection connection;   

    public AppConnection() {
        this.initConnection();
    }

    private void initConnection() {
        try {
            context = new InitialContext();            
            source = (DataSource)context.lookup("jdbc/MySQLDataSource");
            connection = source.getConnection();
        } catch (SQLException ex) {
            Logger.getLogger(AppConnection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(AppConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }        

    public Connection getContextConnection() {
        return connection;
    }

    public void closeContextConnection() throws SQLException {
        if(connection != null) {
            connection.close();
        }
    }
} 

Note that this class is an EJB and "jdbc/MySQLDataSource" is the name of the connection configured on your server. It can be inject in other EJB to get your current connection and create other needed objects as Statements or Resultsets.

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