user2092656
user2092656

Reputation: 11

Mysql database Exception

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();
        }
    }
}

}


Upvotes: 1

Views: 3617

Answers (2)

Daneshkhan
Daneshkhan

Reputation: 41

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();
        }
    }
}

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109567

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);

Upvotes: 4

Related Questions