设为首页 加入收藏

TOP

使用JDBC连接数据库(二)
2014-11-24 00:12:28 来源: 作者: 【 】 浏览:27
Tags:使用 JDBC 连接 数据库
getString("author"); Timestamp time = rs.getTimestamp("createdate"); Timestamp last = rs.getTimestamp("MIDIFYDATE"); System.out.println("新闻id:"+id+" 标题:"+title+" 摘要:"+summary +" 内容:"+content+" 作者:"+author+" 创建时间:"+time+" 修改时间:"+last); } } catch (SQLException e) { e.printStackTrace(); }finally{ try { rs.close(); statement.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } //添加新闻信息 public void add(int id,String title,String summary,String content, String author,Date createdate,Date last){ getConnection(); String sql = "insert into news values( , , , , , , )"; try { pstmt = connection.prepareStatement(sql); pstmt.setInt(1, id); pstmt.setString(2, title); pstmt.setString(3, summary); pstmt.setString(4, content); pstmt.setString(5, author); pstmt.setTimestamp(6, new java.sql.Timestamp(createdate.getTime())); pstmt.setTimestamp(7, new java.sql.Timestamp(last.getTime())); int i = pstmt.executeUpdate();//i表示插入的行数 if(i>0) System.out.println("插入成功!"); else System.out.println("插入失败!"); } catch (SQLException e) { e.printStackTrace(); }finally{ try { pstmt.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }

然后看测试类

import java.util.Date;


public class Test {
	public static void main(String[] args) {
		NewsDao nd = new NewsDao();
		nd.getNewsList();
		nd.add(2, "有蚊子", "今天竟然有蚊子", "今天竟然真的有蚊子,咬了我好几口!",
				"admin", new Date(), new Date());
	}
}

程序正常运行。

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇MongoDB副本集集群 下一篇DBCP数据库配置含义

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: