为了账号安全,请及时绑定邮箱和手机立即绑定

Java jdbc数据库添加数据

Java jdbc数据库添加数据

fenkapian 2016-07-16 11:36:55
下段代码第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()))";你试一下

查看完整回答
反对 回复 2016-07-16
  • fenkapian
    fenkapian
    但又变成了运行时异常,调用add方法插入数据时报错
  • 慕粉3333924
    慕粉3333924
    你要插入的是7个字段,但你前面括号里就写了6个字段,所以出错了
  • 2 回答
  • 0 关注
  • 2401 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信