无法连接到数据库

时间:2012-07-14 17:09:42

标签: java database java-ee

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

1 个答案:

答案 0 :(得分:2)

您需要将oracle.jdbc.OracleDriver添加到您的类路径,Add appropriate version's jar到您的应用程序的类路径