日期:2014-05-20  浏览次数:20713 次

连接数据库异常
/**
 * 插入新的新闻
 * @param id
 * @return
 */
public boolean insert(News news) {
boolean flag = false;
Connection conn = null;
sqlStr = "insert into news (title,author,from,content,postDate) values ('";
sqlStr += news.getNTitle() + "','";
sqlStr += news.getNAuthor() + "','";
sqlStr += news.getNFrom() + "','";
sqlStr += news.getNContent() + "','";
sqlStr += new java.sql.Date(new java.util.Date().getTime()) + "')";
try {
conn = this.getConnection();
pstmt = conn.prepareStatement(sqlStr);
int i = pstmt.executeUpdate(); //<--这里
if(0 != i) {
flag = true;
conn.commit();
}
} catch (SQLException e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
conn.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
return flag;
}
程序运行指定位置代码时发生异常。通过Eclipse的dubug发现提交的查询语句中文部分都乱码了。
重点是在这条代码之前的sqlStr都是正常的,没有出现乱码。
我该怎么解决?

------解决方案--------------------
preparedStatement一般都是用?代替参数