newsupdatingbean.java

来自「已完成的jsp%2BjavaBean%2BServlet实例有增删改查很好的mv」· Java 代码 · 共 132 行

JAVA
132
字号
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 + =
减小字号Ctrl + -
显示快捷键?