Java jdbc数据库添加数据

下段代码第71行究竟哪里错了啊
  1. package cn.pd.news.dao;


  2. import java.sql.Connection;

  3. import java.sql.DriverManager;

  4. import java.sql.PreparedStatement;

  5. import java.sql.ResultSet;

  6. import java.sql.SQLException;

  7. import java.sql.Timestamp;

  8. import java.util.Date;


  9. import cn.pb.news.util.ConfigManager;


  10. public class NewsDao {

  11. public void getNewsList() { 

  12. Connection con = null; 

  13. //Statement st = null;

  14. PreparedStatement st = null;

  15. ResultSet rs = null;

  16. String driver = ConfigManager.getInstance().getString("jdbc.driver.class");

  17. String url = ConfigManager.getInstance().getString("jdbc.connection.url");

  18. String username = ConfigManager.getInstance().getString("jdbc.connection.username");

  19. String password = ConfigManager.getInstance().getString("jdbc.connection.password");

  20. try {

  21. Class.forName(driver);       //url中主机地址也可以是127.0.0.1

  22. con = DriverManager.getConnection(url,username,password);

  23. //System.out.println(con);

  24. String sql = "select * from news_detail";

  25.    //st = con.createStatement();

  26. st = con.prepareStatement(sql);

  27. rs = st.executeQuery(sql);

  28. while(rs.next()) {

  29. int id = rs.getInt("id"); 

  30. int categoryId = rs.getInt("categoryId");

  31. String title = rs.getString("title");

  32. String summary = rs.getString("summary");

  33. String content = rs.getString("content"); 

  34. String author = rs.getString("author");

  35. Timestamp creatDate = rs.getTimestamp("createDate");

  36. System.out.println("Id: " + id + "\t title: " + title + "\t summary: " + summary + 

  37. "\t content: " + content + "\t author: " + author + "\t creatDate" + creatDate );

  38. }

  39. } catch (ClassNotFoundException | SQLException e) {

  40. e.printStackTrace();

  41. }finally{

  42. if(rs != null) {

  43. try {

  44. rs.close();

  45. st.close();

  46. con.close();

  47. } catch (SQLException e) {

  48. e.printStackTrace();

  49. }

  50. }

  51. }

  52. }


  53. public void add(int id,int categoryId,String title,String summary,String content,String author,Date creatDate) {

  54. Connection con = null; 

  55. //Statement st = null;

  56. PreparedStatement ps = null;

  57. ResultSet rs = null;

  58. String driver = ConfigManager.getInstance().getString("jdbc.driver.class");

  59. String url = ConfigManager.getInstance().getString("jdbc.connection.url");

  60. String user = ConfigManager.getInstance().getString("jdbc.connection.username");

  61. String password = ConfigManager.getInstance().getString("jdbc.connection.password");

  62. try {

  63. Class.forName(driver);

  64. con = DriverManager.getConnection(url, user, password);

  65. //String sql = "insert into news_detail (id,categoryId,title,summary,content,author,createDate) values ("+id+","+categoryId+",'"+title+"','"+summary+"','"+content+"','"+new Timestamp(createDate.getTime())+"')";

  66.       String sql = "insert into news_detail(id,categoryId,title,summary,content,createDate) values("+id+","+categoryId+",'"+title+"','"+summary+"','"+content+"','"+author+"','"+new Timestamp(createDate.getTime())+"')";


  67. ps = con.prepareStatement(sql);

  68. int i = ps.executeUpdate(sql);

  69. if( i > 0 ) {

  70. System.out.println("插入成功");

  71. }

  72. } catch (ClassNotFoundException e) {

  73. e.printStackTrace();

  74. } catch (SQLException e) {

  75. e.printStackTrace();

  76. }

  77. }

  78. public static void main(String [] args) {

  79. NewsDao nd = new NewsDao();

  80. nd.getNewsList();

  81. }

  82. }

fenkapian
浏览 2402回答 2
2回答

慕粉3333924

不要写+和“”,直接String sql = "insert into news_detail(id,categoryId,title,summary,content,createDate) values(id,categoryId,title,summary,content,author,new Timestamp(createDate.getTime()))";你试一下
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java