我在這裏有一個小問題。 基本上我想創建一個連接池使用一個類的數據庫。該池可供其他類用於執行查詢。我已經創建了連接類的其他類子類。 這是我到目前爲止。與數據庫的Java連接
Connection類/(連接池類)
import java.sql.*; public class connect extends cPool {
public static void main(String[] args) {
cPool en = new cPool(); //crate an object in the name of cPoll calss
Connection conn = null;
Object data;
data = (connect) conn;
en.readinfo(); //call object then method name
String userName = "root";
String password = "" + en.paword + "";// hold outside try catch block to get and set
String url = "" + en.url + "";
try
{
Class.forName ("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
System.err.println("Tried connecting using" + url + userName + password +"");
}
finally
{
}
}
}
這裏是執行語句類
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class getProduct extends connect {
public static void main(String[] args) {
connect cn = new connect();
Connection conn = cn.data;
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM My_Table");
}
catch (SQLException ex) {
Logger.getLogger(getProduct.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
}
}
}
我不能執行任何語句。從第二課,當我做我得到一個錯誤與createStatement()。它說'不可編譯的源代碼 - 找不到符號' 非常感謝。
那麼問題是什麼?我沒有看到問題。 – duffymo 2011-05-18 09:09:13
和有什麼問題? – Fortega 2011-05-18 09:09:55
我無法執行任何語句。從我做的時候,我得到了createStatement()的錯誤。它說'不可編譯的源代碼 - 找不到符號' – Sunny 2011-05-18 09:13:06