简体   繁体   English

Java JDBC与MySQL的连接

[英]Java JDBC connection to MySQL

I am trying to connect to mysql from java web application in eclipse. 我正在尝试从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());
    }
  }

I am always getting the Exception: com.mysql.jdbc.Driver 我总是收到异常:com.mysql.jdbc.Driver

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

import it to the "java build path/lib" 将其导入“ java构建路径/ lib”

the mysql version is 5.1.3 under. mysql版本是5.1.3下的。

running: 运行:

mysql 5.1.3 (db is up and running queries form PHP) mysql 5.1.3(db已启动并正在通过PHP运行查询)

windows XP Windows XP

Java EE Java EE

you say you placed the JAR on the build path, is it in the runtime class path? 您说您将JAR放在构建路径上,是否在运行时类路径中? you said "java EE", so i assume you are deploying this as a web app? 您说“ java EE”,所以我假设您将其部署为Web应用程序? in that case you need to put the JDBC JAR file into WEB-INF/lib of your web app. 在这种情况下,您需要将JDBC JAR文件放入Web应用程序的WEB-INF / lib中。

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. 您是否也在构建配置中添加了它,如果该位置不在classpath中,那么仅将其保存在lib文件夹中就无法工作。 check your .classpath file 检查您的.classpath文件

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 . 如果下载了MySQL JDBC驱动程序 ,则可以找到有关如何通过任何Web应用程序(JBOSS,Glassfish等...)连接到MySQL数据库的完整文档(第14章)。 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 : 这是有关如何从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();
        }
    }
} 

Note that this class is an EJB and "jdbc/MySQLDataSource" is the name of the connection configured on your server. 请注意,该类是EJB,“ jdbc / MySQLDataSource”是在服务器上配置的连接的名称。 It can be inject in other EJB to get your current connection and create other needed objects as Statements or Resultsets . 可以将其注入其他EJB中以获取当前连接并创建其他所需的对象作为StatementsResultsets

You have to download the mysql jdbc connector und use it as lib. 您必须下载mysql jdbc连接器,并将其用作lib。 You can get it here: 你可以在这里得到它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM