servlet读MySQL数据库的问题
就是用servlet读MySQL数据库的一个单表
//先建一个连数据库的Connection
package link;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.NamingException;
public class DB {
private String driverName="com.mysql.jdbc.Driver";
private String user="root";
private String password="PASSWORD";
private String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk";
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Connection getConnection(){
try{
Class.forName(driverName);
return DriverManager.getConnection(url,user,password);
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
**************************************************************************************************************************************
//读数据库里的数据
package link;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class inquiremysql extends HttpServlet {
/**
* Constructor of the object.
*/
public inquiremysql() {
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 {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print("<table>");
out.print("<tr>");
out.print("<th>学号</th>");
out.print("<th>姓名</th>");
out.print("<th>年龄</th>");
out.print("<th>性别</th>");
out.print("<th>成绩</th>");
out.print("</tr>");
DB conn=new DB();
String sql="select * from stu";
Statement stmt=(Statement) conn.getConnection();
try {
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
out.print("<td>"+(String)rs.getString("sId")+"</td>");
out.print("<td>"+(String)rs.getString("sName")+"</td>");
out.print("<td>"+(String)rs.getString("sAge")+"</td>");
out.print("<td>"+(String)rs.getString("sSex")+"</td>");
out.print("<td>"+(String)rs.getString("sScore")+"</td>");
//response.sendRedirect("look.jsp");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.print("</tr>");
out.print("</table>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @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 doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
**************************************************************************************************************************************
//index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>servlet连数据库</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="link/inquiremysql">点击链接</a>
</body>
</html>
**************************************************************************************************************************************///web.xml,这里面配置了别的servlet在上面的代码里没用到就没贴出来
<?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>
<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>inquiremysql</servlet-name>
<servlet-class>link.inquiremysql</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>myservlet</servlet-name>
<servlet-class>servlet.myservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>inquiremysql</servlet-name>
<url-pattern>/servlet/inquiremysql</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/servlet/myservlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>