下段代码第71行究竟哪里错了啊package cn.pd.news.dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Timestamp;import java.util.Date;import cn.pb.news.util.ConfigManager;public class NewsDao { public void getNewsList() { Connection con = null; //Statement st = null; PreparedStatement st = null; ResultSet rs = null; String driver = ConfigManager.getInstance().getString("jdbc.driver.class"); String url = ConfigManager.getInstance().getString("jdbc.connection.url"); String username = ConfigManager.getInstance().getString("jdbc.connection.username"); String password = ConfigManager.getInstance().getString("jdbc.connection.password"); try { Class.forName(driver); //url中主机地址也可以是127.0.0.1 con = DriverManager.getConnection(url,username,password); //System.out.println(con); String sql = "select * from news_detail"; //st = con.createStatement(); st = con.prepareStatement(sql); rs = st.executeQuery(sql); while(rs.next()) { int id = rs.getInt("id"); int categoryId = rs.getInt("categoryId"); String title = rs.getString("title"); String summary = rs.getString("summary"); String content = rs.getString("content"); String author = rs.getString("author"); Timestamp creatDate = rs.getTimestamp("createDate"); System.out.println("Id: " + id + "\t title: " + title + "\t summary: " + summary + "\t content: " + content + "\t author: " + author + "\t creatDate" + creatDate ); } } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); }finally{ if(rs != null) { try { rs.close(); st.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public void add(int id,int categoryId,String title,String summary,String content,String author,Date creatDate) { Connection con = null; //Statement st = null; PreparedStatement ps = null; ResultSet rs = null; String driver = ConfigManager.getInstance().getString("jdbc.driver.class"); String url = ConfigManager.getInstance().getString("jdbc.connection.url"); String user = ConfigManager.getInstance().getString("jdbc.connection.username"); String password = ConfigManager.getInstance().getString("jdbc.connection.password"); try { Class.forName(driver); con = DriverManager.getConnection(url, user, password); //String sql = "insert into news_detail (id,categoryId,title,summary,content,author,createDate) values ("+id+","+categoryId+",'"+title+"','"+summary+"','"+content+"','"+new Timestamp(createDate.getTime())+"')"; String sql = "insert into news_detail(id,categoryId,title,summary,content,createDate) values("+id+","+categoryId+",'"+title+"','"+summary+"','"+content+"','"+author+"','"+new Timestamp(createDate.getTime())+"')"; ps = con.prepareStatement(sql); int i = ps.executeUpdate(sql); if( i > 0 ) { System.out.println("插入成功"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String [] args) { NewsDao nd = new NewsDao(); nd.getNewsList(); } }
2 回答
慕粉3333924
TA贡献3条经验 获得超1个赞
不要写+和“”,直接String sql = "insert into news_detail(id,categoryId,title,summary,content,createDate) values(id,categoryId,title,summary,content,author,new Timestamp(createDate.getTime()))";你试一下
添加回答
举报
0/150
提交
取消