0
public class Db_try1 {
public static void main(String[] args)
{
String driver="oracle.jdbc.OracleDriver";
String url="jdbc:oracle:thin:@localhost:1521:xe";
String user="admin";
String pass="admin";
String sql="";
Connection conn=null;
try
{
Class.forName(driver);
System.out.println("Connecting to database Student.....");
conn=DriverManager.getConnection(url,user,pass);
Statement stmt = conn.createStatement();
sql="select * from student";
ResultSet rs= stmt.executeQuery(sql);
System.out.println("Reading the data and printing...");
while(rs.next())
{
System.out.println(rs.getInt("id"));
System.out.println(rs.getString(1));
System.out.println(rs.getString("mobile"));
}
rs.close(); stmt.close(); conn.close();
}
catch (Exception e)
{
// TODO Auto-generated catch block
System.out.println("Error found");
e.printStackTrace();
}
}
}
我使用oracle 10g我可以連接到數據庫,當我點擊NetBeans中的連接測試connectin。但是當我運行這段代碼我得到異常ClassNotFoundException的..無法連接到數據庫
我加ojdbc14.jar的....怎麼知道哪一個是適當? – 2012-07-14 17:16:08
這是與您使用的JDK匹配的那個。我認爲ojdbc16.jar會是一個更好的主意。 – duffymo 2012-07-14 17:45:33