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

📄 newsdb.java

📁 JSP+ORCLE 实现后台登陆
💻 JAVA
字号:
package xwfb;

import java.sql.*;


public class newsDB
{
	String DBDriver =  "oracle.jdbc.driver.OracleDriver";
	String DBUrl = "jdbc:oracle:thin:@localhost:1521:ORACLE";
	String DBUser = "jerry";
	String DBPsw = "123456";
	Connection conn = null;
	PreparedStatement stmt = null;
	ResultSet rs = null;

	private Connection initDB()
	{
		try
		{
			Class.forName(DBDriver);
			return DriverManager.getConnection(DBUrl,DBUser,DBPsw);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public int addNews(xwfb.newsTable ntable)
	{
		try
		{
			conn = this.initDB();
			String sql = "select MAX(id) from XWFB_NEWS";
			this.stmt = null;
			this.stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			int d = 0;
			if(rs.next())
			{
				d = rs.getInt(1) + 1;
			}
			sql = "insert into XWFB_NEWS(id,title,content,author,create_time) values(?,?,?,?,?)";
			this.stmt = null;
			this.stmt = conn.prepareStatement(sql);

			stmt.setInt(1,d);
			stmt.setString(2,ntable.getTitle());
			stmt.setString(3,ntable.getContent());
			stmt.setString(4,ntable.getAuthor());
			stmt.setString(5,ntable.getCreatetime());

			stmt.executeUpdate();
			return 1;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return 0;
		}
		finally
		{
			try
			{
				rs.close();
				conn.close();
				stmt.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	public int editNews(xwfb.newsTable ntable)
	{
		try
		{
			conn = this.initDB();
			String sql = "update XWFB_NEWS set title=?,content =?,editor=?,editor_time=?,where id =?";
			this.stmt = null;
			this.stmt = conn.prepareStatement(sql);

			stmt.setString(1,ntable.getTitle());
			stmt.setString(2,ntable.getContent());
			stmt.setString(3,ntable.getEditor());
			stmt.setString(4,ntable.getEditortime());
			stmt.setString(5,ntable.getId());

			stmt.executeUpdate();

			return 1;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return 0;
		}
		finally
		{
			try
			{
				conn.close();
				stmt.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	public int deleteNews(String [] id)
	{
		try
		{
			for(int i=0; i<id.length; i++)
			{
				String sql = "delete from XWFB_NEWS where id ="+id[i];
				this.stmt = null;
				this.stmt = conn.prepareStatement(sql);
				stmt.executeUpdate();

				sql = "delete from XWFB_COMMENT where news_id ="+id[i];
				this.stmt = null;
				this.stmt = conn.prepareStatement(sql);
				stmt.executeUpdate();
			}

			return 1;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return 0;
		}
		finally
		{
			try
			{
				conn.close();
				stmt.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

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