我從我的代碼中收到cannot find symbol
錯誤。有誰知道什麼會導致這個問題?使用Class.forName()時出現「找不到符號」問題()
的代碼是:
// Register JDBC driver
Class.forName("net.sourceforge.jtds.jdbc.Driver");
和誤差輸出爲:
blah.java:314: cannot find symbol
symbol : method forName(java.lang.String)
location: class java.lang.Class
Class.forName("net.sourceforge.jtds.jdbc.Driver");
^
1 error
//STEP 1. Import required packages
import java.sql.*;
public class JDBCExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.sql.jdbc.Driver";
static final String DB_URL = (":jdbc:jtds:sqlserver://localhost:1433/tempdb");
// Database credentials
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("net.sourceforge.jtds.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
//STEP 4: Execute a query
System.out.println("Creating database...");
stmt = conn.createStatement();
String sql = "CREATE DATABASE ";
stmt.executeUpdate(sql);
System.out.println("Database created successfully...");
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye");
}//end main
}//end JDBCExample
請提出問題,請不要只發布錯誤消息。 – luke 2010-06-14 03:51:17
@joseph,我試圖清理這個問題,使它更加負責任。如果我錯誤地改變了意圖,請告訴我。我猜測你真的在使用GWT嗎? – paxdiablo 2010-06-14 04:02:09
@paxdiablo:這個問題已經過度地編輯了。你能否改回它來反映第一個Class.forName ...是代碼的事實,並且從method ... onward開始的所有內容都是編譯器錯誤輸出? – 2010-06-14 04:51:52