1

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from java.sql.Statement to com.mysql.jdbc.Statement

i am a beginner in java i am trying to use mysql database i have downloaded mysql-connector-java-5.1.23-bin.jar file from mysql.com and i have added this jar file to in my build path of my project but there is an error in the following code Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from java.sql.Statement to com.mysql.jdbc.Statement


package com.example.demo;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class DBConnect
{
private static final String userName = "root";
private static final String userpwd = "sverma";
private static final String CONN_STR = "jdbc:mysql://localhost:3306/phpweb_db";

public static void main(String[] args) throws SQLException
{

    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;

    try
    {
        DriverManager.getConnection(CONN_STR, userName, userpwd);
        st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
        rs = st.executeQuery("select * from user");
        rs.last();
        System.out.println("No of rows: " + rs.getRow());

        // System.out.println("Connected Successfully...");
    }
    catch (SQLException e)
    {
        System.err.println(e);

    }
    finally
    {
        if (rs != null)
        {
            rs.close();
        }
        if (st != null)
        {
            st.close();
        }
        if (conn != null)
        {
            conn.close();
        }
    }
}

}


2
  • welcome to SO! you quoted your question. Commented Feb 20, 2013 at 19:44
  • using eclipse? You can delete your import statements and click ctrl + shift + O to automatically import the needed classes. Commented Feb 20, 2013 at 19:48

2 Answers 2

4

Wrong classes

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

should be

import java.sql.Connection;
import java.sql.Statement;

In fact, java decouples everything from a specific database engine. One never should need an import of MySQL (or ProgressSQL or ...) classes.

To have those classes available at run-time, the first thing after the try, before getting the connection would be:

Class.forName("com.mysql.jdbc.Driver");

This technique would allow reading all strings from a configuration file, and writing database independent code.


Missing: conn = ...

conn = DriverManager.getConnection(CONN_STR, userName, userpwd);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Sir for your Reply... I have corrected the above code as you have instructed above but i am getting the error while run time that is ** Exception in thread "main" java.lang.NullPointerException**
0
package com.example.demo;

import java.sql.*;


public class DBConnect
{
private static final String userName = "root";
private static final String userpwd = "sverma";
private static final String CONN_STR = "jdbc:mysql://localhost:3306/phpweb_db";

public static void main(String[] args) throws SQLException
{

    Connection conn;
    Statement st;
    ResultSet rs;

    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        DriverManager.getConnection(CONN_STR, userName, userpwd);
        st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
        rs = st.executeQuery("select * from user");
        rs.last();
        System.out.println("No of rows: " + rs.getRow());

        // System.out.println("Connected Successfully...");
    }
    catch (SQLException e)
    {
        System.err.println(e);

    }
    finally
    {
        if (rs != null)
        {
            rs.close();
        }
        if (st != null)
        {
            st.close();
        }
        if (conn != null)
        {
            conn.close();
        }
    }
}

1 Comment

Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written

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.