⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newsupdatingbean.java

📁 已完成的jsp%2BjavaBean%2BServlet实例有增删改查很好的mvc源代码
💻 JAVA
字号:
package admin.manageBean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class NewsUpdatingBean {
	
	private int id;
	private String newsTitle;
	private String newsContent;
	private String author;
	
	public NewsUpdatingBean() {
		init();
	}
	
	private void init(){
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	private Connection getConnection(){
		
		try {
			return DriverManager.getConnection("jdbc:odbc:news");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public int newsUpdating(){
		Connection cn=getConnection();
		PreparedStatement ps;
		String ustr = "update news set newsTitle=?,newsContent=?,author=? where id=?";
		try {
			ps = cn.prepareStatement(ustr);
			ps.setString(1, this.getNewsTitle());
			ps.setString(2, this.getNewsContent());
			ps.setString(3, this.getAuthor());
			ps.setInt(4, this.getId());
			ps.executeUpdate();
			cn.commit();
			return 1;
		} catch (SQLException e) {
			try {
				cn.rollback();
				return 0;
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			try {
				cn.close();
				cn = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return -1;
	}
		
	public void uNewsQuery(){
		Connection cn = getConnection();
		PreparedStatement ps;
		ResultSet rs;
		String qstr="select * from news where id = ?";
		try {
			ps=cn.prepareStatement(qstr);
			ps.setInt(1, this.getId());
			rs=ps.executeQuery();
			while(rs.next()){
				this.setId(rs.getInt("id"));
				this.setNewsTitle(rs.getString("newsTitle"));
				this.setNewsContent(rs.getString("newsContent"));
				this.setAuthor(rs.getString("author"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				cn.close();
				cn = null;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	
	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getNewsContent() {
		return newsContent;
	}

	public void setNewsContent(String newsContent) {
		this.newsContent = newsContent;
	}

	public String getNewsTitle() {
		return newsTitle;
	}

	public void setNewsTitle(String newsTitle) {
		this.newsTitle = newsTitle;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -