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

📄 bookdao.java

📁 地址本 可记录多个事物
💻 JAVA
字号:
package dao;

import java.sql.*;
import java.util.ArrayList;

import po.BookPO;

public class BookDAO {
	
	private Connection conn = null;
	private Statement state = null;
	private ResultSet rs = null;
	
	//新增
	public boolean saveBook(BookPO book)
	{   
		boolean isok = false;
		conn = Tools.getConnection();
		try {
			conn = Tools.getConnection();
			state = conn.createStatement();
			int i = state.executeUpdate("insert into address values ("+book.getId()+",'"+book.getUsername()+"','"+book.getAddress()+"')");
		    if(i > 0)
		    {
		    	isok = true;
		    }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(state != null)
					state.close();
				if(conn != null)
				    conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return isok;
           }
	
    //修改
	public boolean updateBookPO(BookPO book)
	{
		boolean isok = false;
		conn = Tools.getConnection();
		try {
			conn = Tools.getConnection();
			state = conn.createStatement();
			int i = state.executeUpdate("update address set name = '"+book.getUsername()+"',address ='"+book.getAddress()+"' where id ="+book.getId());
		    if(i > 0)
		    {
		    	isok = true;
		    }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(state != null)
					state.close();
				if(conn != null)
				    conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return isok;
	}
	
	//取得ID
	public int getNextID()
	{
		int myID = 0;
		conn = Tools.getConnection();
		try {
			conn = Tools.getConnection();
			state = conn.createStatement();
			rs = state.executeQuery("select max(id) myID from address");
		    if(rs.next())
		    {
		    	myID = rs.getInt("myID");
		    }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(rs != null)
				    rs.close();
				if(state != null)
					state.close();
				if(conn != null)
				    conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return ++myID;
	}
		
	//查询所有
	public ArrayList findAll()
	{
		ArrayList al = new ArrayList();
		conn = Tools.getConnection();
		try {
			conn = Tools.getConnection();
			state = conn.createStatement();
			rs = state.executeQuery("select * from address");
		    while(rs.next())
		    {   
		    	BookPO book = new BookPO();
		    	book.setId(rs.getInt("id"));
		    	book.setUsername(rs.getString("name"));
		    	book.setAddress(rs.getString("address"));
		    	
		    	al.add(book);
		    }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(rs != null)
				    rs.close();
				if(state != null)
					state.close();
				if(conn != null)
				    conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return al;
	}
	
	//删除一条记录 
	public boolean deleteBookByID(int id)
	{
		boolean isok = false;
		conn = Tools.getConnection();
		try {
			conn = Tools.getConnection();
			state = conn.createStatement();
			int i = state.executeUpdate("delete from address where id = "+id);
		    if(i > 0)
		    {
		    	isok = true;
		    }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(state != null)
					state.close();
				if(conn != null)
				    conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return isok;
	}
	
    //查找一条记录
	public BookPO findBookByID(int id)
	{
		BookPO book = null;
		
		try {
			conn = Tools.getConnection();
			state = conn.createStatement();
			rs = state.executeQuery("select * from address where id ="+id);
		    if(rs.next())
		    {   
		    	book = new BookPO();
		    	book.setId(rs.getInt("id"));
		    	book.setUsername(rs.getString("name"));
		    	book.setAddress(rs.getString("address"));
		    }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try {
				if(rs != null)
				    rs.close();
				if(state != null)
					state.close();
				if(conn != null)
				    conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return book;
	}
}

⌨️ 快捷键说明

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