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

because ("com.mysql.jdbc.Driver") is a correct driver name

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;
            }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.