关闭

简单的查询数据、连接数据库

标签: 数据库
216人阅读 评论(0) 收藏 举报
分类:

工具:postgreSQL,Eclipse
技术:jsp,servlet,jdbc
1.准备数据:

select * from city

这里写图片描述
2.写index.jsp

<%@ 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>
<form action="testServlet" name="test" method="post" >
<input name="testText" type="text" placeholder="搜索城市" value="">
</form>
</body>
</html>

结果如下图:
这里写图片描述
2这时候就需要配置web.xml
结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
    <display-name>testServlet</display-name>
    <servlet-name>testServlet</servlet-name>
    <servlet-class>com.zoey.test.testServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>testServlet</servlet-name>
    <url-pattern>/testServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

3.写testServlet

package com.zoey.test;


import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zoey.bean.testData;
import com.zoey.dao.postgreSQLConn;

public class testServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        String testString = req.getParameter("testText");
        Connection conn = postgreSQLConn.conn();
        Statement st = null;
        ResultSet rs = null;
        ArrayList<testData> data= new ArrayList<testData>();
        try {
            st = conn.createStatement();
            String testS = new String(testString.getBytes("ISO-8859-1"), "utf-8");
            String sql = " select * from city where name='" + testS+"'";
             rs =  st.executeQuery(sql);
             if(rs!=null){
            while(rs.next()){
                testData test = new testData();
                test.setId(rs.getInt(1));
                test.setName(rs.getString(2));
                data.add(test);
                 }
          }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                rs.close();
                st.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            postgreSQLConn.close();
        }
        req.setAttribute("data", data);
        req.getRequestDispatcher("/view.jsp").forward(req, resp);
     }
}

4.写view.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!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>
    <table border="1" width="500px">
        <tr>
            <td>序号</td>
            <td>数据</td>
        </tr>
    <c:forEach items="${data}" var="dataTest" varStatus="vs">
        <tr>
            <td>${dataTest.id}</td>
            <td>${dataTest.name}</td>
        </tr>
    </c:forEach>
    </table>
</body>
</html>

最终结果
这里写图片描述
写完了代码,总结一下,所遇到的问题?
第一:

String sql = " select * from city where name='" + testS+"'";

sql语句中出现乱码:
更改如下:

String testS = new String(testString.getBytes("ISO-8859-1"), "utf-8");

第二:

<c:forEach items="${data}" var="dataTest" varStatus="vs">

不是很清楚这个,看[http://blog.csdn.net/u011008029/article/details/49908421]

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:185788次
    • 积分:3406
    • 等级:
    • 排名:第10936名
    • 原创:175篇
    • 转载:2篇
    • 译文:0篇
    • 评论:41条
    最新评论