基础代码随笔

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">

  <display-name>lyb</display-name>

 <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <filter>

    <filter-name>interEncoding</filter-name>

    <filter-class>com.luojin.entity.FilterOne</filter-class>

    <init-param>

      <param-name>encoding</param-name>

      <param-value>utf-8</param-value>

    </init-param>

  </filter>

  <filter-mapping>

    <filter-name>interEncoding</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <error-page>

    <error-code>404</error-code>

    <location>/error_404.jsp</location>

  </error-page>

  <error-page>

    <error-code>500</error-code>

    <location>/error_500.jsp</location>

  </error-page>

 

  <servlet>

  <servlet-name>login</servlet-name>

  <servlet-class>com.luojin.Servlet.ListServlet</servlet-class>

  </servlet>

  <servlet-mapping>

  <servlet-name>login</servlet-name>

  <url-pattern>/listServlet</url-pattern>

  </servlet-mapping>

</web-app>

 

 

 

 

 

<%@ 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=UTF-8">

<title>Insert title here</title>

</head>

<body>

<script type="text/javascript">

  window.onload=function(){

        window.location.href="listServlet";

  }

</script>

</body>

</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<%@page import="com.luojin.entity.Message"%>

<%@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=UTF-8">

<title>Insert title here</title>

</head>

<body>

<%List<Message>list =(List<Message>)request.getAttribute("list"); %>

</head>

<body>

<form name="talbe1" action="delteserlvet" mothed="post" >

<h1 align="center"> 欢迎进入留言板管理系统</h1>

<table width="80%" border="0" cellpadding="4" cellspacing="1" bgcolor="#464646" class="tablestyle" align="center">

                          <!--   <tr align="center">

                    <td height="40" colspan="5" align="center" bgcolor="#EEEEEE" class="talbetitle"> &nbsp;</td>

                    </tr> -->

                   <tr height="40" bgcolor="#EEEEEE" class="talbetitle" >

                                <td width="10%" align="center">标题</td>

                    <td width="20%" align="center">内容</td>

                    <td width="8%" align="center">作者</td>

                    <td width="10%" align="center">发表时间</td>

                  

                  </tr>

                     <%for(Message land : list) {%>

                    <tr align="center">

                        <td width="10%" align="center" bgcolor="#EEEEEE"><%=land.getTitle()%></td>

                                          <td width="6%"  align="center" bgcolor="#EEEEEE" class="textstyle"><%=land.getContent()%></td>

                                          <td width="20%" align="center" bgcolor="#EEEEEE"><%=land.getAuthor() %></td>

                                          <td width="10%" align="center" bgcolor="#EEEEEE"><%=land.getSendTime()%></td>

                                   </tr>

                                <%}%>

                               <tr align="center">

                    <td colspan="5"  bgcolor="#EEEEEE" ><a href="#" onchick="return testTable()">我要留言</a></td>

                    </tr>

                </table>

     </form>

</body>

</html>

</body>

</html>

 

CSS

.textstyle

{

 font-family:normal;

 font-size:18px;

 font-weight:bold;

 color:orange;

 text-decoration:none;

}

.tablestyle

{

 font-family:normal;

 font-size:18px;

 font-weight:bold;

 text-decoration:none;

}

.talbetitle

{

 background:royalblue;

 padding:5px;

 text-align:left;

 color:white;

 font-family:normal;

 text-align:center

 height:20px;

 font-style:normal;

 font-weight:bold;

 font-size:18px;

 letter-spacing:2px;

 padding-left:10px;

}

 

 

 

 

package com.luojin.dao;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

/**数据服务类 */

public class DBCon {

       final static String  SQLSERVER_DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";

        String username="sa";

        String password="123456";

        String dbName="lyb";

        String db_url="jdbc:sqlserver://127.0.0.1:1433; DatabaseName=" + dbName;

        // 获取数据库连接对象的公共方法

        public Connection getConnection()

        {

               Connection conn = null;

                     try {

                         Class.forName(SQLSERVER_DRIVER);

                         conn = DriverManager.getConnection(db_url, username, password);

                     } catch (ClassNotFoundException e) {

                         e.printStackTrace();

                     } catch (SQLException e) {

                         e.printStackTrace();

                     }

                     return conn;

                  }

                     //关闭连接,释放资源

        public void closeConn(Connection conn) {

                     try {

                         if (conn != null && !conn.isClosed()) {

                            conn.close();

                         }

                     } catch (SQLException e) {

                         e.printStackTrace();

                     }

                  }

               //本类测试能不能拿到资源,连接对象

                  public static void main(String[] args)

                  {

                  DBCon  oper = new DBCon ();

                     System.out.println(oper.getConnection().toString());

                  }

              }

 

 

package com.luojin.dao;

 

import java.sql.Connection;

import java.sql.Date;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

 

import com.luojin.entity.Message;

public class MessageBo {

/**添加留言板方法*/

public boolean addMessage(Message message)

{

       // boolean Addmessage=false;

 

       DBCon dao=new DBCon();

       DBCon dbOperator=new  DBCon();//创建一个连接数据库的工具类

       Connection conn=dbOperator.getConnection();//获取连接的数据库

       String sql="insert into messageboard(title,content,sendTime,email,author) values(?,?,?,?,?)";

 

       try {

              PreparedStatement pst=conn.prepareStatement(sql);

              pst.setString(1, message.getTitle());

              pst.setString(2, message.getContent());

 

              pst.setDate(3, message.getSendTime());

              pst.setString(4, message.getEmail());

              pst.setString(5, message.getAuthor());

              if(pst.executeUpdate()>0){

                     return true;

              }

       } catch (SQLException e) {

              e.printStackTrace();

       }finally{

              dbOperator.closeConn(conn);

       }

       return false;

}

/**查询方法*/

public List<Message> queryAll()

{

       List<Message> List = new ArrayList<Message>();

       DBCon dbOperator = new DBCon();

       Connection conn = dbOperator.getConnection();

       try {

           String sql = "select id,title,content,sendTime,email,author from messageboard";

           PreparedStatement pst=conn.prepareStatement(sql);

           ResultSet rs=pst.executeQuery();

           while(rs.next()){

              int id = rs.getInt("id");

              String title =rs.getString("title");

              String content = rs.getString("content");

              Date sendTime=rs.getDate("sendTime");

              String email =rs.getString("email");

              String author =rs.getString("author");

              Message lists=new Message(id,title,content,sendTime,email,author);

              List.add(lists);

           }

       } catch (Exception e) {

           e.printStackTrace();

       }finally{

           dbOperator.closeConn(conn);

       }

       return List ;

    }

      

}

 

 

 

package com.luojin.dao;

 

import java.sql.Connection;

import java.sql.Date;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

import com.luojin.dao.DBCon;

import com.luojin.entity.User;

/**根据用户名查询*/

public class UserBo {

       public User findUser(String username)

       {

              User user=null;

               DBCon dbOperator=new  DBCon();//创建一个连接数据库的工具类

              Connection conn=dbOperator.getConnection();//获取连接的数据库

              String sql="select password,type,email,birth,sex,telephon from  users where userName=?";

              try {

                     PreparedStatement pst=conn.prepareStatement(sql);//执行SQL语句

                     pst.setString(1, username);

               ResultSet rs=pst.executeQuery();

               if(rs.next())//遍历结果集

               {

               String password=rs.getString("password");

               String type=rs.getString("type");

               String email=rs.getString("email");

               Date birth=rs.getDate("birth");

               String sex=rs.getString("sex");

               String telephon=rs.getString("telephon");

               user=new User(password,type,email,birth,sex,telephon);

               if(user!=null)

               {

                      return user;

               }

               }

              } catch (SQLException e) {

             

                     e.printStackTrace();

              }finally

              {

                     dbOperator.closeConn(conn);//不要忘关闭数据连接对象

              }

              return null;

      

      

       }

      

          

      

      

      

      

/*public User findUser(String username)

{

       User user=null;

        DBCon dbOperator=new  DBCon();//创建一个连接数据库的工具类

       Connection conn=dbOperator.getConnection();//获取连接的数据库

       String sql="select password,type,email,birth,sex,telephon from  users where userName=?";

       try {

              PreparedStatement pst=conn.prepareStatement(sql);//执行SQL语句

              pst.setString(1, username);//设置到preparedstatement要查找的条件

        ResultSet rs=pst.executeQuery();//ResultSet设置一个结果集

        if(rs.next())//遍历结果集

        {

        String password=rs.getString("password");

        String type=rs.getString("type");

        String email=rs.getString("email");

        Date birth=rs.getDate("birth");

        String sex=rs.getString("sex");

        String telephon=rs.getString("telephon");

        user=new User(password,type,email,birth,sex,telephon);

        }

       } catch (SQLException e) {

      

              e.printStackTrace();

       }finally

       {

              dbOperator.closeConn(conn);//不要忘关闭数据连接对象

       }

       return user;

}*/

}

 

 

 

public class ListServlet extends HttpServlet {

       private static final long serialVersionUID =1L;

    protected void service(HttpServletRequest req, HttpServletResponse resp)

          throws ServletException, IOException {

           req.setCharacterEncoding("utf-8");

           resp.setContentType("text/html;charset=utf-8");

           MessageBo dao=new MessageBo();

           List<Message> list= dao.queryAll();

           req.setAttribute("list", list);

           req.getRequestDispatcher("Main.jsp").forward(req, resp);

    }

}

 

 

 

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">

  <display-name>lyb</display-name>

 <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <filter>

    <filter-name>interEncoding</filter-name>

    <filter-class>com.luojin.entity.FilterOne</filter-class>

    <init-param>

      <param-name>encoding</param-name>

      <param-value>utf-8</param-value>

    </init-param>

  </filter>

  <filter-mapping>

    <filter-name>interEncoding</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <error-page>

    <error-code>404</error-code>

    <location>/error_404.jsp</location>

  </error-page>

  <error-page>

    <error-code>500</error-code>

    <location>/error_500.jsp</location>

  </error-page>

 

  <servlet>

  <servlet-name>login</servlet-name>

  <servlet-class>com.luojin.Servlet.ListServlet</servlet-class>

  </servlet>

  <servlet-mapping>

  <servlet-name>login</servlet-name>

  <url-pattern>/listServlet</url-pattern>

  </servlet-mapping>

</web-app>

你可能感兴趣的:(简单查询功能)