Android連接服務器數據庫查詢+Gson解析(List和Map)格式json數據(一)


1、服務器端代碼

package com.pdsu.book.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
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;

import com.google.gson.Gson;
import com.pdsu.book.user.Book;

public class SelectServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SelectServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=lib_books;user=sa;password=1234";// sa身份連接

		String bookinfo = request.getParameter("bookinfo");
		System.out.println(bookinfo);
		// String password = request.getParameter("password");
		// String username = "xiaoming";
		// String password = "xiaoming";
		String result = "";
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		StringBuilder builder = new StringBuilder();
		try {
			// Establish the connection.
			System.out.println("begin.");
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			con = DriverManager.getConnection(url);
			System.out.println("end.");

			// Create and execute an SQL statement that returns some data.
			String SQL = "select * from book where book_ISBN='" + bookinfo
					+ "'";
			stmt = con.createStatement();
			rs = stmt.executeQuery(SQL);

			// Iterate through the data in the result set and display it.
			List<Book> books = new ArrayList<Book>();
			while (rs.next()) {
				// System.out.println(rs.getString(2) + " " + rs.getString(1));
				// builder.append(rs.getString("username") +
				// ">>>>>>>>"+rs.getString("password"));

				Book book = new Book(rs.getString("book_name"),
						rs.getString("book_id"), rs.getString("book_saveNum"),
						rs.getString("book_price"), rs.getString("book_desc"),
						rs.getString("book_location"),
						rs.getString("book_author"), rs.getString("book_press"),
						rs.getString("book_ISBN"), rs.getString("book_pressDate"),
						rs.getString("book_type"), rs.getString("book_num"));

				// (String bookName, String bookId, String bookSaveNum, String
				// bookPrice, String bookDesc, String bookLocation, String
				// bookAuthor, String bookPress, String bookISBN, String
				// bookPressDate, String bookType, String bookNum)

				books.add(book);
			}
			Gson gson = new Gson();
			result = gson.toJson(books);
		}

		// Handle any errors that may have occurred.
		catch (Exception e) {
			e.printStackTrace();
		}

		finally {
			if (rs != null)
				try {
					rs.close();
				} catch (Exception e) {
				}
			if (stmt != null)
				try {
					stmt.close();
				} catch (Exception e) {
				}
			if (con != null)
				try {
					con.close();
				} catch (Exception e) {
				}
		}

		out.println(result);
		// System.out.println(result);
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

2、web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<servlet>
		<servlet-name>bookselect</servlet-name>
		<servlet-class>com.pdsu.book.servlet.BookSelectServlet</servlet-class>
	</servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>BookDetailInfoServlet</servlet-name>
    <servlet-class>com.pdsu.book.servlet.BookDetailInfoServlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>BookNoticeInfo</servlet-name>
    <servlet-class>com.pdsu.book.servlet.BookNoticeInfo</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>LinkSQLServerTest</servlet-name>
    <servlet-class>com.pdsu.book.servlet.LinkSQLServerTest</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>LoginUserServlet</servlet-name>
    <servlet-class>com.pdsu.book.servlet.LoginUserServlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>SelectServlet</servlet-name>
    <servlet-class>com.pdsu.book.servlet.SelectServlet</servlet-class>
  </servlet>





	<servlet-mapping>
		<servlet-name>bookselect</servlet-name>
		<url-pattern>/bookselect</url-pattern>
	</servlet-mapping>
  <servlet-mapping>
    <servlet-name>BookDetailInfoServlet</servlet-name>
    <url-pattern>/BookDetail</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>BookNoticeInfo</servlet-name>
    <url-pattern>/notice</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>LinkSQLServerTest</servlet-name>
    <url-pattern>/test</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>LoginUserServlet</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>SelectServlet</servlet-name>
    <url-pattern>/select</url-pattern>
  </servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<login-config>
		<auth-method>BASIC</auth-method>
	</login-config>
</web-app>

3、Book實體類Class

package com.pdsu.book.user;

public class Book {

	private String bookName;
	private String bookId;
	private String bookSaveNum;

	public String getBookSaveNum() {
		return bookSaveNum;
	}

	public void setBookSaveNum(String bookSaveNum) {
		this.bookSaveNum = bookSaveNum;
	}

	private String bookPrice;
	private String bookDesc;
	private String bookLocation;
	// 作者
	private String bookAuthor;
	// 出版社
	private String bookPress;
	private String bookISBN;
	private String bookPressDate;

	private String bookType;
	private String bookNum;

	public Book() {
		super();
	}

	public Book(String bookName, String bookId, String bookSaveNum,
			String bookPrice, String bookDesc, String bookLocation,
			String bookAuthor, String bookPress, String bookISBN,
			String bookPressDate, String bookType, String bookNum) {
		super();
		this.bookName = bookName;
		this.bookId = bookId;
		this.bookSaveNum = bookSaveNum;
		this.bookPrice = bookPrice;
		this.bookDesc = bookDesc;
		this.bookLocation = bookLocation;
		this.bookAuthor = bookAuthor;
		this.bookPress = bookPress;
		this.bookISBN = bookISBN;
		this.bookPressDate = bookPressDate;
		this.bookType = bookType;
		this.bookNum = bookNum;
	}

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public String getBookId() {
		return bookId;
	}

	public void setBookId(String bookId) {
		this.bookId = bookId;
	}

	public String getBookPrice() {
		return bookPrice;
	}

	public void setBookPrice(String bookPrice) {
		this.bookPrice = bookPrice;
	}

	public String getBookDesc() {
		return bookDesc;
	}

	public void setBookDesc(String bookDesc) {
		this.bookDesc = bookDesc;
	}

	public String getBookLocation() {
		return bookLocation;
	}

	public void setBookLocation(String bookLocation) {
		this.bookLocation = bookLocation;
	}

	public String getBookAuthor() {
		return bookAuthor;
	}

	public void setBookAuthor(String bookAuthor) {
		this.bookAuthor = bookAuthor;
	}

	public String getBookPress() {
		return bookPress;
	}

	public void setBookPress(String bookPress) {
		this.bookPress = bookPress;
	}

	public String getBookISBN() {
		return bookISBN;
	}

	public void setBookISBN(String bookISBN) {
		this.bookISBN = bookISBN;
	}

	public String getBookPressDate() {
		return bookPressDate;
	}

	public void setBookPressDate(String bookPressDate) {
		this.bookPressDate = bookPressDate;
	}

	public String getBookType() {
		return bookType;
	}

	public void setBookType(String bookType) {
		this.bookType = bookType;
	}

	public String getBookNum() {
		return bookNum;
	}

	public void setBookNum(String bookNum) {
		this.bookNum = bookNum;
	}

}

發佈了24 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章