javaWEB項目與普通java項目在鏈接oracle數據庫上彷佛略有不一樣。java
普通java項目直接將ojdbc.jar構建路徑便可;web
而javaweb項目卻可能並不能運行,須要再設置一下:sql
項目名上右鍵點擊→屬性→Deployment Assembly→Add→java Build Path Entries →ojdbc.jar,最後肯定,從新運行項目。數據庫
package dal; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class OracleHelper { static final String DB_USER="sys as sysdba"; static final String DB_PWD="******"; static final String ORCL_URL="jdbc:oracle:thin:@localhost:1521/orcl"; public String getUser(String findEmpno){ Connection con=null; PreparedStatement pre=null; ResultSet result=null; String nameInOrcl=null; try{ Class.forName("oracle.jdbc.driver.OracleDriver"); con=DriverManager.getConnection(ORCL_URL,DB_USER,DB_PWD); String sql="select empname from t_emp where empno=?"; pre=con.prepareStatement(sql); pre.setString(1,findEmpno); result=pre.executeQuery(); while(result.next()){ nameInOrcl=result.getString("empname"); } }catch(Exception ex){ System.out.println("未能返回結果"); }finally{ try { result.close(); } catch (SQLException e) { System.out.println("關閉記錄集失敗"); } try { pre.close(); } catch (SQLException e) { System.out.println("關閉控制器失敗"); } try { con.close(); } catch (SQLException e) { System.out.println("關閉鏈接失敗"); } } return nameInOrcl; } }