【问题标题】:Mysql database ExceptionMysql数据库异常
【发布时间】:2017-12-29 17:27:48
【问题描述】:

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

}


【问题讨论】:

  • 欢迎来到 SO!你引用了你的问题。
  • 使用日食?您可以删除您的导入语句并单击ctrl + shift + O 以自动导入所需的类。

标签: java


【解决方案1】:

错误的课程

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

【讨论】:

  • 感谢先生您的回复...我已经按照您上面的指示更正了上面的代码,但是在运行时出现错误,即线程“main”java.lang 中的**异常。 NullPointerException**
【解决方案2】:
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();
        }
    }
}

【讨论】:

  • 不鼓励仅使用代码回答,因为它们没有为未来的读者提供太多信息,请对您所写的内容提供一些解释
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-17
  • 2017-03-10
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多