131: String intro =new String(request.getParameter("intro").trim().getBytes("iso8859_1"));哪里有问题
时间: 2024-03-24 17:41:26 浏览: 115
这段代码在获取 request 参数 "intro" 的值后,首先将其转换为 iso8859_1 编码的字节数组,然后再通过 new String() 方法将其转换回字符串。这种转换可能会导致乱码问题,因为在 iso8859_1 编码中可能无法表示一些字符。
更好的做法是在获取参数时直接使用 UTF-8 编码,例如:
```
String intro = request.getParameter("intro");
intro = new String(intro.getBytes("ISO-8859-1"), "UTF-8").trim();
```
这样可以确保获取的参数值不会出现乱码问题。
相关问题
class Node: def __init__(self, num, name, intro): self.num = num self.name = name self.intro = intro self.visited = False class Edge: def __init__(self, fr, to, weight): self.fr = fr self.to = to self.weight = weight class Graph: def __init__(self): self.nodes = [] self.edges = {} def add_node(self, node): self.nodes.append(node) self.edges[node] = [] def add_edge(self, edge): self.edges[edge.fr].append(edge) self.edges[edge.to].append(Edge(edge.to, edge.fr, edge.weight))graph = Graph() with open('data.txt', 'r', encoding='utf-8') as f: for line in f.readlines(): items = line.strip().split(',') num = int(items[0]) name = items[1] intro = items[2] node = Node(num, name, intro) graph.add_node(node) for i in range(3, len(items), 2): neighbor = int(items[i]) weight = int(items[i+1]) edge = Edge(node, graph.nodes[neighbor-1], weight) graph.add_edge(edge) data.txt里怎么写
data.txt 的格式应该是每一行代表一个节点和它的邻居节点以及边的权重,具体格式如下:
节点编号,节点名称,节点介绍,邻居节点1,边的权重1,邻居节点2,边的权重2,...
例如:
1,Node1,This is Node1,2,10,3,15,4,20
2,Node2,This is Node2,1,10,3,5
3,Node3,This is Node3,1,15,2,5,4,8
4,Node4,This is Node4,1,20,3,8
public List<Movie> getPageData(int startindex, int pagesize, String category_id){ try { QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "select * from movie where category_id=? limit ?,?"; Object params[] = {category_id, startindex, pagesize}; return (List<Movie>)runner.query(sql, params, new BeanListHandler(Movie.class)); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } public int getTotalRecord(String category_id){ try { QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "select count(*) from movie where category_id=?"; long totalrecord = (Long)runner.query(sql, category_id, new ScalarHandler()); return (int)totalrecord; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } //(id,name,author,price,image,description,category_id) @Override public void update(Movie movie) { try { QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "update movie set name=?,actor=?,price=?,poster=?,intro=?,category_id=? where id=?"; Object params[] = { movie.getName(), movie.getActor(), movie.getPrice(), movie.getPoster(), movie.getIntro(), movie.getCategory_id(),movie.getId()}; runner.update(sql, params); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } @Override public int delete(String id) { try { QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); //执行删除的sql runner.update("delete from movie where id=?",id); } catch (SQLException e) { throw new RuntimeException(e);//抛出运行异常 } return 1;//删除成功返回1表示结束 }帮我加一下注释
好的,以下是代码的注释:
```
// 获取指定类别下的电影列表(分页)
public List<Movie> getPageData(int startindex, int pagesize, String category_id){
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 获取数据库连接
String sql = "select * from movie where category_id=? limit ?,?"; // 查询语句
Object params[] = {category_id, startindex, pagesize}; // 查询参数
return (List<Movie>)runner.query(sql, params, new BeanListHandler(Movie.class)); // 执行查询并返回结果
} catch (Exception e) {
e.printStackTrace(); // 打印异常信息
throw new RuntimeException(e); // 抛出运行时异常
}
}
// 获取指定类别下的电影总数
public int getTotalRecord(String category_id){
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 获取数据库连接
String sql = "select count(*) from movie where category_id=?"; // 查询语句
long totalrecord = (Long)runner.query(sql, category_id, new ScalarHandler()); // 执行查询并返回结果
return (int)totalrecord; // 将结果转换为整型并返回
} catch (Exception e) {
e.printStackTrace(); // 打印异常信息
throw new RuntimeException(e); // 抛出运行时异常
}
}
// 更新电影信息
@Override
public void update(Movie movie) {
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 获取数据库连接
String sql = "update movie set name=?,actor=?,price=?,poster=?,intro=?,category_id=? where id=?"; // 更新语句
Object params[] = { movie.getName(), movie.getActor(), movie.getPrice(), movie.getPoster(), movie.getIntro(), movie.getCategory_id(),movie.getId()}; // 更新参数
runner.update(sql, params); // 执行更新操作
} catch (SQLException e) {
e.printStackTrace(); // 打印异常信息
throw new RuntimeException(e); // 抛出运行时异常
}
}
// 删除电影信息
@Override
public int delete(String id) {
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 获取数据库连接
//执行删除的sql语句
runner.update("delete from movie where id=?",id); // 删除操作
} catch (SQLException e) {
throw new RuntimeException(e); // 抛出运行时异常
}
return 1; // 删除成功返回1表示结束
}
```
阅读全文