尝试将Mysql用于我的Java程序时出错

时间:2011-01-26 03:39:11

标签: java

我在尝试编译以下代码时遇到此错误。我怀疑代码不支持MySQL。我想使用MySQL并尝试修改它,但它无法编译。

QuadrantSystemRDB.java:183: cannot find symbol
symbol  : variable driverName
location:class edu.indiana.iucbrf.examples.quadrantRDBTemplate.QuadrantSystemRD
B
        dbInfo = new DBInfo(new JDBCDriverInfo(driverName, url, username, passwo
rd),constantsTableName);
                                               ^
Note: QuadrantSystemRDB.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

代码:

    private void setupInfo() {

    Driver driver = new com.mysql.jdbc.Driver();
    String url = "jdbc:mysql://localhost:3306/test";
    String username = "root";
    String password = "123456";

    String problemFeatureSpecTableName = "ProblemFeatureSpec";
    String solutionFeatureSpectTableName = "SolutionFeatureSpec";
    String classTableName = "Class";
    String extraDataTableName = "ExtraData";
    String casebaseTablename = "CaseBase";
    String problemTableName = "Problem";
    String solutionTableName = "Solution";
    String inactiveContextsTableName = "InactiveContext";
    String constantsTableName = "Constants";
    dbInfo = new DBInfo(new JDBCDriverInfo(driverName, url, username, password),constantsTableName);
    problemSpecInfo = new FeatureSpecRDBInfo(problemFeatureSpecTableName, classTableName, extraDataTableName);
    solutionSpecInfo = new FeatureSpecRDBInfo(solutionFeatureSpectTableName, classTableName, extraDataTableName);
    rdbCasebaseInfo = new RDBCaseBaseInfo(casebaseTablename, solutionTableName, problemTableName, inactiveContextsTableName);
}

编辑:

这是我的回音类路径 - >

C:\ Documents and Settings \ user> echo%classpath% 。; C:\ Program Files \ Java \ jre6 \ lib \ ext \ QTJava.zip; C:\ iucbrf \; C:\ mysql-connector-ja VA-5.1.14-bin.jar; C:\ iucbrf.jar

编辑2:

我将driverName更改为driver。 现在错误是......

 QuadrantSystemRDB.java:167: unreported exception java.sql.SQLException; must be
 caught or declared to be thrown
            Driver driver = new com.mysql.jdbc.Driver();
                            ^
 Note: QuadrantSystemRDB.java uses unchecked or unsafe operations.
 Note: Recompile with -Xlint:unchecked for details.
 1 error

编辑3:

try
  {
    Driver driver = new com.mysql.jdbc.Driver();
    String url = "jdbc:mysql://localhost:3306/test";
    String username = "root";
    String password = "123456";

catch (Exception e)
  {
   // message.
  }

3 个答案:

答案 0 :(得分:1)

你在哪里声明变量“driverName”?

答案 1 :(得分:1)

您初始化但从不使用driver

您使用driverName但从未对其进行初始化。

解决方案就像将driverName更改为driver一样简单吗? :)

修改

QuadrantSystemRDB.java:167: unreported exception java.sql.SQLException; must be
caught or declared to be thrown
    Driver driver = new com.mysql.jdbc.Driver();

我认为com.mysql.jdbc.Driver()可以抛出异常;您需要将其包装在try/catch块中,或者声明您的方法抛出相同的异常。您选择哪一个导致更清晰的设计,但简单的try/catch可以让编译很快成功。 :)

答案 2 :(得分:0)

我注意到的第一件事是在这一行:

dbInfo = new DBInfo(new JDBCDriverInfo(driverName, url, username, password), constantsTableName);

在哪里声明了driverName?