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 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();
				}
		}

  

转载于:https://www.cnblogs.com/I-will-be-different/p/3425817.html

你可能感兴趣的:(jdbc连接数据库代码)