01_11_SERVLET中使用javabean

01_11_SERVLET中使用javabean

1. javabean

广义javabean = 普通java

狭义javabean = 符合 Sun JavaBean标准的类

Servlet中使用Bean和在通常程序中使用Bean类似

属性名称第一个字母必须小写,一般private

比如,private productId

一般具有getters and setters

要具有一个参数为空的构造方法

Bean不应具有GUI表现

一般是用来实现某一业务逻辑或取得特定结果

2. 示例

2.1封装的javabean

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

 

 

public class DB {

 

public static Connection getConn () {

Connection conn = null;

try {

Class.forName("com.mysql.jdbc.Driver");

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/world?user=root&password=123456");

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

return conn;

}

 

public static Statement getStatement(Connection conn) {

Statement stmt = null;

if (conn != null) {

try {

stmt = conn.createStatement();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return stmt;

}

 

public static ResultSet getResultSet(Statement stmt, String sql) {

ResultSet rs = null;

if (stmt != null && sql != "") {

try {

rs = stmt.executeQuery(sql);

} catch (SQLException e) {

e.printStackTrace();

}

}

return rs;

}

 

public static void closeStmt(Statement stmt) {

if (stmt != null) {

try {

stmt.close();

stmt = null;

} catch (SQLException e) {

e.printStackTrace();

}

 

}

}

 

public static void closeConn(Connection conn) {

if (conn != null) {

try {

conn.close();

conn = null;

} catch (SQLException e) {

e.printStackTrace();

}

 

}

}

public static void closeRs(ResultSet rs) {

if (rs != null) {

try {

rs.close();

rs = null;

} catch (SQLException e) {

e.printStackTrace();

}

 

}

}

}

2.2展示

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

Connection conn = null;

Statement stmt = null;

ResultSet rs = null;

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

PrintWriter out = response.getWriter();

out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("<HTML>");

out.println("<HEAD><TITLE>Servlet连接MySQL数据库</TITLE></HEAD>");

out.println("<BODY>");

out.print("<table align=\"center\" border=\"1\"><tr align=\"center\"><td>查询world库中city表中的Name列信息</td></tr>");

try {

conn = DB.getConn();

stmt = DB.getStatement(conn);

rs = DB.getResultSet(stmt, "select * from city");

while (rs.next()) {

out.println("<tr align=\"center\"><td>" +rs.getString("name") + "</td></tr>");

}

out.println("</table>");

} catch (SQLException e) {

e.printStackTrace();

} finally {

if (rs != null) {

try {

rs.close();

rs = null;

} catch (SQLException e) {

e.printStackTrace();

}

 

}

if (stmt != null) {

try {

stmt.close();

stmt = null;

} catch (SQLException e) {

e.printStackTrace();

}

 

}

if (conn != null) {

try {

conn.close();

conn = null;

} catch (SQLException e) {

e.printStackTrace();

}

 

}

 

}

 

out.println("</BODY>");

out.println("</HTML>");

out.flush();

out.close();

}

 

posted @ 2018-04-14 07:02  FlyBack  阅读(507)  评论(0编辑  收藏  举报