美好的一天从搞崩eclipse开始,也许是我昨天看了追了部电视剧,向涵之真好看,程序员不配拥有爱情(我不信)。
言归正传,一个下午还是有收获的复习了JDBC,然后编写了servlet的一个重点。
index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>index page</title> </head> <body> <a href="./system/list">显示员工信息</a> </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <servlet> <servlet-name>listEmpServlet</servlet-name> <servlet-class>web.listEmpServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>listEmpServlet</servlet-name> <url-pattern>/system/list</url-pattern> </servlet-mapping> </web-app>
listEmpServlet.java
package web; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class listEmpServlet implements Servlet { @Override public void destroy() { } @Override public ServletConfig getServletConfig() { return null; } @Override public String getServletInfo() { return null; } @Override public void init(ServletConfig arg0) throws ServletException { } @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.print("<html>"); out.print("<head>"); out.print("<meta charset='UTF-8'>"); out.print("<title>员工信息</title>"); out.print("</head>"); out.print("<body>"); out.print("<h3 align='center'>员工列表</h3>"); out.print("<hr width='60%'>"); out.print("<table border="1" align="center" width="50%">"); out.print("<tr align="center">"); out.print("<th>序号</th>"); out.print("<th>员工编号</th>"); out.print("<th>员工姓名</th>"); out.print("<th>员工薪水</th>"); out.print("</tr>"); // JDBC Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/MySQL", "root", "root"); String sql = "select empno,ename,sal from login.emp"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); int i = 0; while (rs.next()) { String empno = rs.getString("empno"); String ename = rs.getString("ename"); String sal = rs.getString("sal"); out.print("<tr align="center">"); out.print("<th>" + ++i + "</th>"); out.print("<th>" + empno + "</th>"); out.print("<th>" + ename + "</th>"); out.print("<th>" + sal + "</th>"); out.print("</tr>"); } } catch (Exception e) { e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } out.print("</table>"); out.print("</body>"); out.print("</html>"); } }