为了起到代码练习效果,这次的代码纯由自己书写,顺便起到复习java基本语法的作用。
1、准备实体类novel
2、准备dao类
封装好增删改查显示等方法,便于以后调用。主要用的就是sql语句、Statement、PreparedStatement的相关方法实现方法。
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import bean.novel;
public class noveldao {
public noveldao() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8", "root",
"123456");
}
public int getTotal() {
int cnt = 0;
try (Connection c = getConnection(); Statement s = c.createStatement();) {
String sql = "select count(*) from novel";
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
cnt ++;
}
} catch (SQLException e) {
e.printStackTrace();
}
return cnt;
}
public void add(novel character) {
String sql = "insert into hero values(null,?,?,?)";
try (Connection c = getConnection(); PreparedStatement ps = c.prepareStatement(sql);)
{
ps.setString(1, character.name);
ps.setString(2, character.area);
ps.setString(3, character.task);
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void update(novel character) {
String sql = "update hero set name= ?, hp = ? , damage = ? where id = ?";
try (Connection c = getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setString(1, character.name);
ps.setString(2, character.area);
ps.setString(3, character.task);
ps.setInt(4, character.id);
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void delete(int id) {
try (Connection c = getConnection(); Statement s = c.createStatement();) {
String sql = "delete from hero where id = " + id;
s.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public novel get(int id) {
novel character = null;
try (Connection c = getConnection(); Statement s = c.createStatement();) {
String sql = "select * from hero where id = " + id;
ResultSet rs = s.executeQuery(sql);
if (rs.next()) {
character = new novel();
String name = rs.getString(2);
String area = rs.getString(3);
String task = rs.getString(4);
character.id = id;
character.name = name;
character.area = area;
character.task = task;
}
} catch (SQLException e) {
e.printStackTrace();
}
return character;
}
public List<novel> list() {
return list(0, Short.MAX_VALUE);
}
public List<novel> list(int start, int count) {
List<novel> characters = new ArrayList<novel>();
String sql = "select * from hero order by id desc limit ?,? ";
try (Connection c = getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1, start);
ps.setInt(2, count);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
novel character = new novel();
int id = rs.getInt(1);
String name = rs.getString(2);
String area = rs.getString(3);
String task = rs.getString(4);
character.id = id;
character.name = name;
character.area = area;
character.task = task;
characters.add(character);
}
} catch (SQLException e) {
e.printStackTrace();
}
return characters;
}
}
3、编写显示表的servlet
package servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.novel;
import dao.noveldao;
public class LList extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
List<novel> characters = new noveldao().list();
StringBuffer sb = new StringBuffer();
sb.append("<table align='center' border='1' cellspacing='0'>\r\n");
sb.append("<tr><td>id</td><td>name</td><td>area</td><td>task</td></tr>\r\n");
String trFormat = "<tr><td>%d</td><td>%s</td><td>%s</td><td>%s</td></tr>\r\n";
for (novel n : characters) {
String tr = String.format(trFormat, n.getid(), n.getname(), n.getarea(), n.gettask());
sb.append(tr);
}
sb.append("</table>");
response.getWriter().write(sb.toString());
}
}
然后配置web
到这一步应该完成任务了,结果运行之后显示
调试了很久都没有解决,明天继续