java 数据库操作_在Java中如何对数据库中的数据进行操作?

展开全部

package com.dao;import java.sql.*;import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.sql.DataSource;public class BaseDao {

/**

* 创建数据库62616964757a686964616fe59b9ee7ad9431333332633063连接及关闭

*/

// 打开连接

public static Connection getConnection() {

Connection con = null; /*************************** oracl 的连接 ***************************************/

// try { // Class.forName("oracle.jdbc.driver.OracleDriver");

// con = DriverManager.getConnection(

// "jdbc:oracle:thin:@127.0.0.1:1521:orcl", "bbs", "sa");

// } catch (ClassNotFoundException e) {

// e.printStackTrace();

// } catch (SQLException e) {

// e.printStackTrace();

// }

/******************************* sqlerver 的连接 ******************************/

try {

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

con = DriverManager.getConnection(

"jdbc:sqlserver://127.0.0.1:1433;databasename=bbs", "sa",

"zhou");

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

/*********************************************************************/

return con;

} // 关闭

public static void closeAll(Connection connection,

PreparedStatement pStatement, ResultSet res) {

try {

if (connection != null && (!connection.isClosed())) {

connection.close();

}

if (res != null) {

res.close();

res = null;

}

if (pStatement != null) {

pStatement.close();

pStatement = null;

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

对数据库增删改查package com.dao;import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;import com.entity.News;public class NewsDao {

Connection con = null;

PreparedStatement pstmt = null;

ResultSet rs = null;

/**

* 添加新闻

* @param news

* @return

*/

public boolean newsAdd(News news){

boolean result=false;

String sql="insert into news values(?,?)";

con=BaseDao.getConnection();

try {

pstmt=con.prepareStatement(sql);

pstmt.setString(1, news.getContent());

pstmt.setString(2, FormatTime.newTime());

int i = 0;

i = pstmt.executeUpdate();

if (i > 0)

result = true;

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return result;

}

/**

* 修改新闻

* @param news

* @return

*/

public boolean updateNews(News news){

boolean result=false;

con=BaseDao.getConnection();

try {

pstmt=con.prepareStatement("update news set content=? ,writedate=? where newsid=?");

pstmt.setString(1, news.getContent());

pstmt.setString(2, FormatTime.newTime());

pstmt.setInt(3, news.getNewsID());

int i = 0;

i = pstmt.executeUpdate();

if (i > 0)

result = true;

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return result;

}

/**

* 删除新闻

* @param news

* @return

*/

public boolean deleteNews(News news){

boolean result=false;

String sql=String.format("delete from news where newsid=%d", news.getNewsID());

con=BaseDao.getConnection();

try {

pstmt=con.prepareStatement(sql);

int i = 0;

i = pstmt.executeUpdate();

if (i > 0)

result = true;

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return result;

}

/**

* 删除新闻 重载

* @param newsId

* @return

*/

public boolean deleteNews(int newsId){

boolean result=false;

String sql=String.format("delete from news where newsid=%d", newsId);

con=BaseDao.getConnection();

try {

pstmt=con.prepareStatement(sql);

int i = 0;

i = pstmt.executeUpdate();

if (i > 0)

result = true;

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return result;

}

/**

* 查询所有的新闻

* @return

*/

public List selectAllNews(){

List list=new ArrayList();

String sql="select * from Users";

con=BaseDao.getConnection();

try {

pstmt=con.prepareStatement(sql);

rs=pstmt.executeQuery();

while(rs.next()){

News news=new News();

news.setNewsID(rs.getInt(1));

news.setContent(rs.getString(2));

news.setWriteDate(rs.getString(3));

list.add(news);

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

BaseDao.closeAll(rs, pstmt, con);

}

return list;

}

/**

* 查询单个

* @return

*/

public News selectOneNews(){

News news=new News();

con=BaseDao.getConnection();

try {

pstmt=con.prepareStatement("select top 1 * from news order by newsid desc");

rs=pstmt.executeQuery();

while(rs.next()){

news.setNewsID(rs.getInt(1));

news.setContent(rs.getString(2));

news.setWriteDate(rs.getString(3));

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

BaseDao.closeAll(rs, pstmt, con);

}

return news;

}

}

实体类package com.entity;import java.io.Serializable;

public class News implements Serializable{

private int newsID;

private String content;

private String writeDate; public News() {

super();

// TODO Auto-generated constructor stub

} public News(String content, String writeDate) {

super();

this.content = content;

this.writeDate = writeDate;

} public News(int newsID, String content, String writeDate) {

super();

this.newsID = newsID;

this.content = content;

this.writeDate = writeDate;

} public int getNewsID() {

return newsID;

} public void setNewsID(int newsID) {

this.newsID = newsID;

} public String getContent() {

return content;

} public void setContent(String content) {

this.content = content;

} public String getWriteDate() {

return writeDate;

} public void setWriteDate(String writeDate) {

this.writeDate = writeDate;

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值