我在尝试编译以下代码时遇到此错误。我怀疑该代码不支持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.
}
最佳答案
您初始化但从未使用驱动程序
。
您使用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 block 中,或者声明您的方法抛出相同的异常。您选择哪一个会带来更简洁的设计,但简单的 try/catch
可以让编译非常快速地成功。 :)
关于java - 尝试将 Mysql 用于我的 Java 程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4801213/