Java - 线程“主”java.lang.Error 中的异常:未解决的编译问题

Posted

技术标签:

【中文标题】Java - 线程“主”java.lang.Error 中的异常:未解决的编译问题【英文标题】:Java - Exception in thread "main" java.lang.Error: Unresolved compilation problems 【发布时间】:2012-06-28 18:23:53 【问题描述】:

我的 JDBC 代码有一些问题。我正在尝试通过 mysql 连接,但它给了我一个错误。如果您有时间,我的错误日志如下所示。

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    BLOB cannot be resolved to a type
    BLOB cannot be resolved to a type

    at serialize.SerializeDeserialze.main(SerializeDeserialze.java:73)

我的代码如下。我正在使用mysql-connector-java-5.1.20.jar驱动程序:

package serialize;
/*
 By vivek
*/

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import oracle.sql.BLOB;

/****
CREATE TABLE java_objects (object_id NUMBER, object_name varchar(128), object_value BLOB DEFAULT empty_blob(), primary key (object_id));
SQL> desc java_objects;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 OBJECT_ID                                 NOT NULL NUMBER
 OBJECT_NAME                                        VARCHAR2(128)
 OBJECT_VALUE                                       BLOB

SQL> select SEQUENCE_NAME, MIN_VALUE, MAX_VALUE, INCREMENT_BY, LAST_NUMBER from  user_sequences;

SEQUENCE_NAME                   MIN_VALUE  MAX_VALUE INCREMENT_BY LAST_NUMBER
------------------------------ ---------- ---------- ------------ -----------
ID_SEQ                                  1 1.0000E+27            1          21
JAVA_OBJECT_SEQUENCE                    1 1.0000E+27            1           1

*/
public class SerializeDeserialze 
  public static void main(String[] args) throws Exception 
    String WRITE_OBJECT_SQL = "BEGIN "
        + "  INSERT INTO java_objects(object_id, object_name, object_value) "
        + "  VALUES (?, ?, empty_blob()) " + "  RETURN object_value INTO ?; " + "END;";
    String READ_OBJECT_SQL = "SELECT object_value FROM java_objects WHERE object_id = ?";

    Connection conn = getOracleConnection();
    conn.setAutoCommit(false);
    List<Object> list = new ArrayList<Object>();
    list.add("This is a short string.");
    list.add(new Integer(1234));
    list.add(new java.util.Date());

    // write object to Oracle
    long id = 0001;
    String className = list.getClass().getName();
    CallableStatement cstmt = conn.prepareCall(WRITE_OBJECT_SQL);

    cstmt.setLong(1, id);
    cstmt.setString(2, className);

    cstmt.registerOutParameter(3, java.sql.Types.BLOB);

    cstmt.executeUpdate();
    BLOB blob = (BLOB) cstmt.getBlob(3);
    OutputStream os = blob.getBinaryOutputStream();
    ObjectOutputStream oop = new ObjectOutputStream(os);
    oop.writeObject(list);
    oop.flush();
    oop.close();
    os.close();

    // Read object from oracle
    PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL);
    pstmt.setLong(1, id);
    ResultSet rs = pstmt.executeQuery();
    rs.next();
    InputStream is = rs.getBlob(1).getBinaryStream();
    ObjectInputStream oip = new ObjectInputStream(is);
    Object object = oip.readObject();
    className = object.getClass().getName();
    oip.close();
    is.close();
    rs.close();
    pstmt.close();
    conn.commit();

    // de-serialize list a java object from a given objectID
    List listFromDatabase = (List) object;
    System.out.println("[After De-Serialization] list=" + listFromDatabase);
    conn.close();
  

  private static Connection getHSQLConnection() throws Exception 
    Class.forName("org.hsqldb.jdbcDriver");
    System.out.println("Driver Loaded.");
    String url = "jdbc:hsqldb:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  

  public static Connection getMySqlConnection() throws Exception 
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/demo2s";
    String username = "oost";
    String password = "oost";

    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  

  public static Connection getOracleConnection() throws Exception 
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
    String username = "userName";
    String password = "password";

    Class.forName(driver); // load Oracle driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  

【问题讨论】:

你的类路径中有 oracle jar 吗? 不,我只将 mysql jar 放在我的类路径中..... 【参考方案1】:

当您使用 Eclipse 作为 IDE 并尝试运行甚至无法编译的代码时会发生此错误。检查 Eclipse 中的 Problems 视图,并在执行应用程序之前修复编译错误。

【讨论】:

谢谢,就是这样。当我在 package 语句之前添加一个 import 语句时,这发生在我身上。【参考方案2】:

您正在使用 MySQL 驱动程序,但您的类使用 Oracle BLOB 类。要么使用 Oracle JDBC 驱动程序,要么为 BLOB 使用不同的类。

由于您的代码提到了 HSQL、MySQL 和 Oracle,您可能希望摆脱特定于 Oracle 的 BLOB 实现。

【讨论】:

以上是关于Java - 线程“主”java.lang.Error 中的异常:未解决的编译问题的主要内容,如果未能解决你的问题,请参考以下文章

如何实现java主线程等待子线程执行完毕之后再执行

java 子线程如何返回结果给主线程

java 多线程子线程唤醒主线程问题

Java多线程--让主线程等待子线程执行完毕

如何从主线程超时java线程?

Java 抛出异常是怎么回事?谢谢....望能提供答案........