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 中单击 test connectin 时,我可以连接到数据库。但是当我运行这段代码时,我得到了异常 classnotfoundexception ..

4

1 回答 1

2

您需要添加oracle.jdbc.OracleDriver到您的类路径中,将适当版本的 jar 添加到应用程序的类路径中

于 2012-07-14T17:12:42.180 回答