jdbc连接数据库代码
try { //1、加载驱动 Class.forName("oracle.jdbc.OracleDriver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //2、创建连接 Connection conn = null; try { conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger"); //jdbc:mysql://localhost:3306/myProject } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } PreparedStatement pstmt = null; List<ExitFinanceBean> list1 = new ArrayList<ExitFinanceBean>(); try { String sql = ""; pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ ExitFinanceBean bean = new ExitFinanceBean(); bean.setE_id(rs.getInt("e_id")); bean.setName(rs.getString("e_name")); list1.add(bean); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { if(pstmt!=null||conn!=null){ pstmt.close(); conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }