debug了很久,一方面sql语句的细节出现了问题,另一方面我怀疑站长提供的jar包太low了,我换了一个用
对上次的封装的方法sql语句错误做出了修改:
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 = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return cnt;
}
public void add(novel character) {
String sql = "insert into novel 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 novel 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 novel 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 novel 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 novel 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;
}
}