* 数据库处理以及在 Servlet 中使用 Bean *
Know@05 Servlet 如何连接数据库
bbs.sqlcreate database bbs;
use bbs;
set names gbk; -- 解决乱码问题
drop table if exists article;
示例程序 : ShowRs.java
Know@06 在 Servlet 中使用 Java_Bean
广义javabean = 普通java类
狭义javabean = 符合Sun JavaBean标准的类
(为了在图形化界面编程的组件可以捞来捞去而提出的javabean的概念,按标准规则来写)
在Servlet中使用Bean和在通常程序中使用Bean类似
*1), 属性名称第一个字母必须小写,一般private, 比如:private productId
*2), 一般具有getters and setters
*3), 要具有一个参数为空的构造方法
*4), 但Bean不应具有GUI表现
*5), 一般是用来实现某一业务逻辑或取得特定结果
将数据库操作封装成一个类DB (非常好的思路,但是这个还封装得不够好)。
示例 : ShowRsUseBean.java
java适合做中间件,但是不太适合开发图形化的程序,但是也有用开发的。
ShowRs.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
//1.仔细检查连接串。
//2.确认有没有这个表
//3.如果出错了。先去后台看报错的信息。
//4.确认mysql已经启动了。
//5.把jdbc的驱动拷贝到tomcat
public class ShowRs extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
response.setContentType("text/html");
response.setCharacterEncoding("gb2312"); // 分开写也不错!
PrintWriter out = response.getWriter();
out.println("<table border=1>");
out.println("<tr><td>Content:</td></tr>");
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager
.getConnection("jdbc:mysql://localhost/bbs?user=root&password=root");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from article");
while (rs.next()) {
out.println("<tr>");
out.println("<td>" + rs.getString("cont") + "</td>");
out.println("</tr>");
}
out.println("</table>");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
DB.java
import java.sql.*;
public class DB {
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/bbs?user=root&password=root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Statement getStatement(Connection conn) {
Statement stmt = null;
try {
if(conn != null) {
stmt = conn.createStatement();
}
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public static ResultSet getResultSet(Statement stmt, String sql) {
ResultSet rs = null;
try {
if(stmt != null) {
rs = stmt.executeQuery(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static void closeConn(Connection conn) {
try {
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeStmt(Statement stmt) {
try {
if(stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeRs(ResultSet rs) {
try {
if(rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
ShowRsUseBean.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class ShowRsUseBean extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("gb2312");
PrintWriter out = response.getWriter();
out.println("<table border=1>");
out.println("<tr><td>Content:</td></tr>");
Connection conn = DB.getConn();
Statement stmt = DB.getStatement(conn);
String sql = "select * from t";
ResultSet rs = DB.getResultSet(stmt, sql);
try {
while (rs.next()) {
out.println("<tr>");
out.println("<td>" + rs.getString("title") + "</td>");
out.println("</tr>");
}
out.println("</table>");
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.closeRs(rs);
DB.closeStmt(stmt);
DB.closeConn(conn);
}
}
}