编写代码如下:
public class Test {
static public String driverOracle = "oracle.jdbc.driver.OracleDriver";
static public String url = "odbc:jdbc:thin:@localhost:1521:orcl";
static public String username = "scott";
static public String password = "abc123"; public static void main(String[] args) {
try {
System.out.println("1");
Class.forName(driverOracle);
System.out.println("2");
// Connection conn = DriverManager.getConnection(url, username,
// password);
System.out.println("3");
} catch (ClassNotFoundException e) {
System.out.println("4");
e.printStackTrace();
System.out.println("5");
}
// catch (SQLException e) {
// System.out.println("6");
// e.printStackTrace();
// System.out.println("7");
// }
}
}
执行结果:
1
2
3
取消注释后运行结果:
1
java.sql.SQLException: No suitable driver found for odbc:jdbc:thin:@localhost:1521:orcl
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Test.main(Test.java:16)
2
6
7这是什么原因啊?我已经将classes12.jar导入项目啦啊!
新手学习中,望高手帮助.
public class Test {
static public String driverOracle = "oracle.jdbc.driver.OracleDriver";
static public String url = "odbc:jdbc:thin:@localhost:1521:orcl";
static public String username = "scott";
static public String password = "abc123"; public static void main(String[] args) {
try {
System.out.println("1");
Class.forName(driverOracle);
System.out.println("2");
// Connection conn = DriverManager.getConnection(url, username,
// password);
System.out.println("3");
} catch (ClassNotFoundException e) {
System.out.println("4");
e.printStackTrace();
System.out.println("5");
}
// catch (SQLException e) {
// System.out.println("6");
// e.printStackTrace();
// System.out.println("7");
// }
}
}
执行结果:
1
2
3
取消注释后运行结果:
1
java.sql.SQLException: No suitable driver found for odbc:jdbc:thin:@localhost:1521:orcl
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Test.main(Test.java:16)
2
6
7这是什么原因啊?我已经将classes12.jar导入项目啦啊!
新手学习中,望高手帮助.
classes12.jar 是不是我不记得了
你在orcle客户端的bin文件找下lib文件夹,
把那个下面的.jar文件倒进工程,应该就可以了其实我也是个菜鸟啊,
一起学习
从异常来看应该是JDBC驱动没加,把jar包加进去
改完这两点再试试,应该没问题了
没有合适的驱动被发现,重新换个包
这句错误了,
应该为:
static public String url = "jdbc:oracle:thin:@localhost:1521:orcl";
8楼的回答能够解决问题,再次感谢.
你的程序里面的内容,是采用ODBC-JDBC桥,来连接数据库的。那么,楼主还需要在你的机器上配置ODBC。比较赞同4楼兄弟的设置方式。就是直接使用JDBC来连接数据库。配置简单,仅需要你说的那个Jar包就行。