简体   繁体   中英

Java JDBC connection to MySQL

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

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.

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

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 .

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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