JDBC(查询编号在1-2之间的所有数据)

   
	public static List<User>selectByID(){
		List<User>list=new ArrayList<User>();  
		Connection connection=getConnection();
		try {
			PreparedStatement pt=connection.prepareStatement("select id,usname,phone,email from user where id in(1,2)");
			ResultSet  rs=pt.executeQuery();
			while(rs.next()){
			 User user=new User();		 
			 user.setId(rs.getInt(1));
			 user.setUsname(rs.getString(2));
			 user.setPhone(rs.getString(3));
			 user.setEmail(rs.getString(4));
			 list.add(user);
 
		}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    return list; 
	} 


	public static  Connection getConnection(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://localhost:3306/db";
			String usname="root";
			String uspwd="root";
			Connection connection=DriverManager.getConnection(url, usname, uspwd);
			if(connection!=null){
				return connection;
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
<%@page import="com.controlle.UserControoler"%>
<%@page import="com.pojo.User"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>

<body>
	<table>
		<tr>
			<td>ID</td>
			<td>Name</td>
			<td>phone</td>
			<td>email</td>
		</tr>
		<%
		 List<User>list=UserControoler.selectByID();
			for (int i = 0; i < list.size(); i++) {
				User use = new User();
				use = (User) list.get(i);
		%>
		<tr>
			<td><%=use.getId()%></td>
			<td><%=use.getUsname()%></td>
			<td><%=use.getPhone()%></td>
			<td><%=use.getEmail()%></td>
		</tr>
		<%
			}
		%>
	</table>
</body>
</html>


猜你喜欢

转载自blog.csdn.net/qq_35002313/article/details/79850824