2

I have used the following code to connect between java and mysql, but it does not find the JDBC_DRIVER. How can I solve this?

Here is the code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/*This code connects to the mysql database*/
public class mysqlconn 
{
    public Connection con = null;
    public Statement statement= null;
    public String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    public String username = "root",password = "taskinn432175";
    static String dbname = "jdbc:mysql://localhost/shopkeeper";
    String sql="select * from shopkeeper";
    String dbtime;

    public  mysqlconn(String userd , String passd){
        this.password = passd;
        this.username= userd;
    }

    public void Connect() {
        try {
            Class.forName(JDBC_DRIVER);
            con = DriverManager.getConnection(dbname,username,password);
            statement= con.createStatement();
            System.out.println ("Database connection established");
            System.out.println("capturing from database");
            ResultSet rs=statement.executeQuery(sql);
            while (rs.next()) {
                dbtime = rs.getString(1);
                System.out.println(dbtime);
            }
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(null,"not connected"+e.getMessage());
        }
    }

    public static void main( String args[] )
    {
        mysqlconn conn=new mysqlconn("taskin","taskinn432175");
        conn.Connect();
    }
}
Svante
  • 50,694
  • 11
  • 78
  • 122
Taskin
  • 41
  • 2
  • 7

2 Answers2

1

Make sure you have the JDBC drivers for mySql in your classpath.

One way to do this (for a simple test program like this):

java -cp .;path_to_jdbc_connector; mysqlconn
Nivas
  • 18,126
  • 4
  • 62
  • 76
0

you must use mysql connector (.jar) file along with this program while running it, you can also resolve this problem by creating DSN for your application.

satu
  • 1