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..
最佳答案
您需要将 oracle.jdbc.OracleDriver
添加到您的类路径,Add appropriate version's jar到您的应用程序的类路径
关于java - 无法连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11485731/