资源简介
从数据库读取下拉列表数据,按顺序显示到下拉列表框里
代码片段和文件信息
package com.lyq.bean;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class CWFindServlet
*/
public class ZLFindServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request HttpServletResponse response) throws ServletException IOException {
// TODO Auto-generated method stub
try {
// 锟斤拷锟斤拷锟斤拷锟捷匡拷锟斤拷锟斤拷锟斤拷注锟结到锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
Class.forName(“com.mysql.jdbc.Driver“);
// 锟斤拷锟捷匡拷锟斤拷锟斤拷锟街凤拷锟斤拷
String url = “jdbc:mysql://localhost:3306/mysql“;
// 锟斤拷锟捷匡拷锟矫伙拷锟斤拷
String username = “root“;
// 锟斤拷锟捷匡拷锟斤拷锟斤拷
String password = “123“;
// 锟斤拷锟斤拷Connection锟斤拷锟斤拷
Connection conn = DriverManager.getConnection(urlusernamepassword);
// 锟斤拷取Statement
Statement stmt = conn.createStatement();
// 锟斤拷锟酵硷拷锟斤拷锟较拷锟絊QL锟斤拷锟?
String sql = “select * from zl_info“;
// 执锟叫诧拷询
ResultSet rs = stmt.executeQuery(sql);
// 实锟斤拷锟斤拷List锟斤拷锟斤拷
List list = new ArrayList();
// 锟叫断癸拷锟斤拷锟斤拷锟狡讹拷锟斤拷锟斤拷锟叫讹拷锟角凤拷锟斤拷效
while(rs.next()){
// 实锟斤拷锟斤拷Book锟斤拷锟斤拷name
ZL_Data book = new ZL_Data();
// 锟斤拷id锟斤拷锟皆革拷值
book.setId(rs.getString(“id“));
book.setModules(rs.getString(“modules“));
// 锟斤拷price锟斤拷锟皆革拷值
book.setCategory(rs.getString(“category“));
// 锟斤拷bookCount锟斤拷锟皆革拷值
list.add(book);
}
// 锟斤拷图锟介集锟较凤拷锟矫碉拷request之锟斤拷
request.setAttribute(“list“ list);
rs.close(); // 锟截憋拷ResultSet
stmt.close(); // 锟截憋拷Statement
conn.close(); // 锟截憋拷Connection
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// 锟斤拷锟斤拷转锟斤拷锟斤拷book_list.jsp
//request.getRequestDispatcher(“lbcx.jsp“).forward(request response);
request.getRequestDispatcher(“index.jsp“).forward(request response);
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1025 2017-12-20 18:39 index.jsp
文件 538 2017-11-13 21:35 ZL_Data.java
文件 2564 2017-11-13 21:37 ZLFindServlet.java
----------- --------- ---------- ----- ----
4127 3
评论
共有 条评论