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();
    }
}
4
  • 5
    Just put it in the runtime classpath. Here's a mini tutorial. Commented Oct 16, 2011 at 16:38
  • Is your mysql driver in your classpath? Commented Oct 16, 2011 at 16:38
  • @rmarimon: exceptions don't lie. Commented Oct 16, 2011 at 16:42
  • Apparently is the same issue that: <stackoverflow.com/questions/2839391/…> Please check the answer. Commented Oct 16, 2011 at 20:13

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.