jdbc 연결 데이터베이스 코드

1511 단어
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 list1 = new ArrayList();
		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();
				}
		}

좋은 웹페이지 즐겨찾기