PreparedStatement与Statement:
1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程
2.使用 Statement 对象。在对数据库只执行一次性存取的时侯,用 Statement 对象进行处理。PreparedStatement 对象的开销比Statement大,对于一次性操作并不会带来额外的好处。
3.statement每次执行sql语句,相关数据库都要执行sql语句的编译,preparedstatement是预编译得, preparedstatement支持批处理
SQL查找单个用户:select
//查找数据
public User selectUserById(int userId){
User user= null;
Connection conn=null;
PreparedStatement ps = null;
ResultSet rs=null;
String sql = "select * from user where userId=?";
try {
conn=DBConnectionUtil.getConnection();
ps=conn.prepareStatement(sql);
ps.setInt(1, userId);
//接收结果集
rs = ps.executeQuery();
//游标(游标往下执行,当有数据时就会执行,没有数据就不执行)
if(rs.next())
{
user = new User();
user.setUserId(rs.getInt("userId"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setSex(rs.getInt("sex"));
user.setFlag(rs.getInt("flag"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBConnectionUtil.closeAll2(rs,ps, conn);
}
return user;
}
查找多个用户:
public List<User> getAllUserList(){
List<User> userList = new ArrayList<User>();
User user= null;
Connection conn=null;
PreparedStatement ps = null;
ResultSet rs=null;
String sql = "select * from user";
try {
conn=DBConnectionUtil.getConnection();
ps=conn.prepareStatement(sql);
//接收结果集
rs = ps.executeQuery();
//游标(游标往下执行,当有数据时就会执行,没有数据就不执行)
while(rs.next())
{
user = new User();
user.setUserId(rs.getInt("userId"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setSex(rs.getInt("sex"));
user.setFlag(rs.getInt("flag"));
userList.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBConnectionUtil.closeAll2(rs,ps, conn);
}
return userList;
}
servlet:
一.创建Servlet(init(),doGet(),doPost())
1.@Override
public void init() throws ServletException {
System.out.println("First初始化");
super.init();
}
2.public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
3.public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
二、配置web.xml
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>com.jkx.web.servlet.FirstServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/firstServlet</url-pattern>
</servlet-mapping>
request作用域是一次使用,而session作用域在该次会话一直有效,浏览器关闭或超过时间或换浏览器或session关闭都失效。
1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程
2.使用 Statement 对象。在对数据库只执行一次性存取的时侯,用 Statement 对象进行处理。PreparedStatement 对象的开销比Statement大,对于一次性操作并不会带来额外的好处。
3.statement每次执行sql语句,相关数据库都要执行sql语句的编译,preparedstatement是预编译得, preparedstatement支持批处理
SQL查找单个用户:select
//查找数据
public User selectUserById(int userId){
User user= null;
Connection conn=null;
PreparedStatement ps = null;
ResultSet rs=null;
String sql = "select * from user where userId=?";
try {
conn=DBConnectionUtil.getConnection();
ps=conn.prepareStatement(sql);
ps.setInt(1, userId);
//接收结果集
rs = ps.executeQuery();
//游标(游标往下执行,当有数据时就会执行,没有数据就不执行)
if(rs.next())
{
user = new User();
user.setUserId(rs.getInt("userId"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setSex(rs.getInt("sex"));
user.setFlag(rs.getInt("flag"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBConnectionUtil.closeAll2(rs,ps, conn);
}
return user;
}
查找多个用户:
public List<User> getAllUserList(){
List<User> userList = new ArrayList<User>();
User user= null;
Connection conn=null;
PreparedStatement ps = null;
ResultSet rs=null;
String sql = "select * from user";
try {
conn=DBConnectionUtil.getConnection();
ps=conn.prepareStatement(sql);
//接收结果集
rs = ps.executeQuery();
//游标(游标往下执行,当有数据时就会执行,没有数据就不执行)
while(rs.next())
{
user = new User();
user.setUserId(rs.getInt("userId"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setSex(rs.getInt("sex"));
user.setFlag(rs.getInt("flag"));
userList.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBConnectionUtil.closeAll2(rs,ps, conn);
}
return userList;
}
servlet:
一.创建Servlet(init(),doGet(),doPost())
1.@Override
public void init() throws ServletException {
System.out.println("First初始化");
super.init();
}
2.public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
3.public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
二、配置web.xml
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>com.jkx.web.servlet.FirstServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/firstServlet</url-pattern>
</servlet-mapping>
request作用域是一次使用,而session作用域在该次会话一直有效,浏览器关闭或超过时间或换浏览器或session关闭都失效。