0

Hi.....


How to access the MySql using servlet....
Now am using Mysql 5.0 & MyOdbc 3.51
What type of connector to be used?...
We used the following syntax..is it correct?or not?

Class.forName("org.gjt.mm.mysql.Driver");
Connection cn=DriverManager.getConnection("jdbc:mysql://192.168.1.38:3306/suba","root","erp");

Whenever i execute this program i got the error like this
ClassNotFoundException: org.gjt.mm.mysql.Driver

3
Contributors
3
Replies
4
Views
11 Years
Discussion Span
Last Post by tyson.crouch
0

Instead of using MyODBC 3.51 use MySQL JDBC Driver - mysql-connector-java-5.1.6-bin

Then use the following servlet code:

import javax.swing.*;
import java.sql.*;

public class Database {

    private Connection connection;
    private static String databaseName = "";
    private static String databaseUsername = "";
    private static String databasePassword = "";
    private static String driver = "com.mysql.jdbc.Driver";

    public Connection getConnection() {
        createConnection();
        return connection;
    }

    public void createConnection() {
        if (connection == null) {
            try {
                Class.forName(driver);
                String url = "jdbc:mysql://localhost/" + databaseName;

                connection = DriverManager.getConnection(
                        url, databaseUsername, databasePassword);

            } catch (Exception e) {
                JOptionPane.showMessageDialog(new JFrame(),
                        "Database connection could not be established." +
                        "Please check your network connection and restart.",
                        "Error Message", JOptionPane.ERROR_MESSAGE);
                e.printStackTrace();
                connection = null;
            }
        }
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.