1

线程“main”java.lang.Error 中的异常:未解决的编译问题:类型不匹配:无法从 java.sql.Statement 转换为 com.mysql.jdbc.Statement

我是java的初学者我正在尝试使用mysql数据库我已经从mysql.com下载了mysql-connector-java-5.1.23-bin.jar文件并且我已经将此jar文件添加到我的项目的构建路径中但是以下代码中有错误 线程“main”java.lang.Error中的异常:未解决的编译问题:类型不匹配:无法从java.sql.Statement转换为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();
        }
    }
}

}


4

2 回答 2

4

错误的课程

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

应该

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

事实上,java 将所有内容与特定的数据库引擎解耦。永远不需要导入 MySQL(或 ProgressSQL 或 ...)类。

为了让这些类在运行时可用,在try, 之后获得连接之前的第一件事是:

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

这种技术将允许从配置文件中读取所有字符串,并编写独立于数据库的代码。


缺少:conn = ...

conn = DriverManager.getConnection(CONN_STR, userName, userpwd);
于 2013-02-20T19:46:16.743 回答
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();
        }
    }
}
于 2017-12-29T09:27:48.207 回答