这是我的java代码
public static Map<String,String> propertyFileReader() {
Map<String, String> map=new HashMap<String, String>();
Properties prop = new Properties();
try {
InputStream inputStream = Util.class.getClassLoader().getResourceAsStream("jdbc.properties");
prop.load(inputStream);
final String DB_DRIVER= prop.getProperty("DB_DRIVER");
final String DB_CONNECTION = prop.getProperty("DB_CONNECTION2");
final String DB_USER = prop.getProperty("DB_USER");
final String DB_PASSWORD = prop.getProperty("DB_PASSWORD");
map.put("DB_DRIVER",DB_DRIVER);
map.put("DB_CONNECTION",DB_CONNECTION);
map.put("DB_USER",DB_USER);
map.put("DB_PASSWORD",DB_PASSWORD);
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
private static Connection getDBConnection() {
Map<String , String > map=new HashMap<String, String>();
map=propertyFileReader();
String DB_DRIVER=map.get("DB_DRIVER");
String DB_CONNECTION= map.get("DB_CONNECTION");
String DB_USER=map.get("DB_USER");
String DB_PASSWORD=map.get("DB_PASSWORD");
Connection dbConnection = null;
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
dbConnection = DriverManager.getConnection(DB_CONNECTION,DB_USER,DB_PASSWORD);
return dbConnection;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
这是我的属性文件
DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
DB_CONNECTION2 = "jdbc:oracle:thin:@10.2.5.23:1521:dbslic";
DB_USER = "TSR_MOBILE";
DB_PASSWORD = "TSR_MOBILE";
我将ojdbc6.jar添加到我的buildpath [尝试使用ojdbc14.jar]
但是有一个错误,如下所示
No suitable driver found for "jdbc:oracle:thin:@10.2.5.23:1521:dbslic";
"oracle/jdbc/driver/OracleDriver";
No suitable driver found for "jdbc:oracle:thin:@10.2.5.23:1521:dbslic";
[ERROR] Exception occurred while trying to invoke service method loginBlock
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:212)
at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:117)
at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:114)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:181)
at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:172)
at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:146)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at mainService.TSR_WEB_SERVICE.loginBlock(TSR_WEB_SERVICE.java:416)
... 25 more
请帮我解决这个问题
答案 0 :(得分:10)
您使用了错误的包来引用OracleDriver
类
改变
oracle.jdbc.driver.OracleDriver
到
oracle.jdbc.OracleDriver
From oracle docs课程定义
public class OracleDriver
extends oracle.jdbc.driver.OracleDriver
实现java.sql.Driver
接口的Oracle JDBC驱动程序类。
答案 1 :(得分:5)
将其用作Oracle驱动程序
oracle.jdbc.OracleDriver
而不是
oracle.jdbc.driver.OracleDriver
答案 2 :(得分:0)
这个答案太迟了,但是被接受的答案是不正确的,所以我把这个帖子发布给任何不幸的人,试图复制/粘贴代码并想知道出了什么问题。
到目前为止,公认的答案是正确的
oracle.jdbc.driver.OracleDriver
已过时,但仍可在2020年使用。所发布代码的问题在于,属性文件使用分号和引号(not valid)。因此,从更改属性文件
DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
DB_CONNECTION2 = "jdbc:oracle:thin:@10.2.5.23:1521:dbslic";
DB_USER = "TSR_MOBILE";
DB_PASSWORD = "TSR_MOBILE";
到
DB_DRIVER = oracle.jdbc.driver.OracleDriver
DB_CONNECTION2 = jdbc:oracle:thin:@10.2.5.23:1521:dbslic
DB_USER = SR_MOBILE
DB_PASSWORD = TSR_MOBILE
使错误消失。