`

jdbc操作oracle数据库(增删改查)

阅读更多

DAOFactory.java

package cn.mldn.lxh.factory ;

import cn.mldn.lxh.dao.* ;
import cn.mldn.lxh.dao.impl.* ;

public class DAOFactory
{
	public static PersonDAO getPersonDAOInstance()
	{
		return new PersonDAOImpl() ;
	}
};

 

DataBaseConnection.java

package cn.mldn.lxh.dbc ;
import java.sql.* ;

// 主要功能就是连接数据库、关闭数据库
public class DataBaseConnection
{
	private final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
	private final String DBURL = "jdbc:oracle:thin:@localhost:1521:MLDN" ;
	private final String DBUSER = "scott" ;
	private final String DBPASSWORD = "tiger" ;
	private Connection conn = null ;

	public DataBaseConnection()
	{
		try
		{
			Class.forName(DBDRIVER) ;
			this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;	
		}
		catch (Exception e)
		{
		}
	}

	// 取得数据库连接
	public Connection getConnection()
	{
		return this.conn ;
	}

	// 关闭数据库连接
	public void close()
	{
		try
		{
			this.conn.close() ;
		}
		catch (Exception e)
		{
		}		
	}
};
 

Person.java

package cn.mldn.lxh.vo ;

// 值对象,包含属性,setter,getter方法
public class Person
{
	private String id ;
	private String name ;
	private String password ;
	private int age ;
	private String email ;

	// 生成getter、setter方法
	public void setId(String id)
	{
		this.id = id ;
	}
	public void setName(String name)
	{
		this.name = name ;
	}
	public void setPassword(String password)
	{
		this.password = password ;
	}
	public void setAge(int age)
	{
		this.age = age ;
	}
	public void setEmail(String email)
	{
		this.email = email ;
	}
	public String getId()
	{
		return this.id ;
	}
	public String getName()
	{
		return this.name ;
	}
	public String getPassword()
	{
		return this.password ;
	}
	public int getAge()
	{
		return this.age ;
	}
	public String getEmail()
	{
		return this.email ;
	}
};

 

PersonDAO.java

package cn.mldn.lxh.dao ;

import java.util.* ;
import cn.mldn.lxh.vo.* ;

// 规定出了操作person表在此项目里的全部方法
public interface PersonDAO
{
	// 增加操作
	public void insert(Person person) throws Exception ;
	// 修改操作
	public void update(Person person) throws Exception ;
	// 删除操作
	public void delete(String id) throws Exception ;
	// 按ID查询操作
	public Person queryById(String id) throws Exception ;
	// 查询全部
	public List queryAll() throws Exception ;
	// 模糊查询
	public List queryByLike(String cond) throws Exception ;
}

 PersonDAOImpl.java

package cn.mldn.lxh.dao.impl ;
import java.sql.* ;
import java.util.* ;
import cn.mldn.lxh.vo.* ;
import cn.mldn.lxh.dbc.* ;
import cn.mldn.lxh.dao.* ;

// 此类需要完成具体的数据库操作,需要JDB代码
public class PersonDAOImpl implements PersonDAO
{
	// 增加操作
	public void insert(Person person) throws Exception
	{
		String sql = "INSERT INTO person (id,name,password,age,email) VALUES (?,?,?,?,?)" ;
		PreparedStatement pstmt = null ;
		DataBaseConnection dbc = null ;

		// 下面是针对数据库的具体操作
		try
		{
			// 连接数据库
			dbc = new DataBaseConnection() ;
			pstmt = dbc.getConnection().prepareStatement(sql) ;
			pstmt.setString(1,person.getId()) ;
			pstmt.setString(2,person.getName()) ;
			pstmt.setString(3,person.getPassword()) ;
			pstmt.setInt(4,person.getAge()) ;
			pstmt.setString(5,person.getEmail()) ;
			// 进行数据库更新操作
			pstmt.executeUpdate() ;
			pstmt.close() ;
		}
		catch (Exception e)
		{
			throw new Exception("操作出现异常") ;
		}
		finally
		{
			// 关闭数据库连接
			dbc.close() ;
		}
	}
	// 修改操作
	public void update(Person person) throws Exception
	{
		String sql = "UPDATE person SET name=?,password=?,age=?,email=? WHERE id=?" ;
		PreparedStatement pstmt = null ;
		DataBaseConnection dbc = null ;

		// 下面是针对数据库的具体操作
		try
		{
			// 连接数据库
			dbc = new DataBaseConnection() ;
			pstmt = dbc.getConnection().prepareStatement(sql) ;			
			pstmt.setString(1,person.getName()) ;
			pstmt.setString(2,person.getPassword()) ;
			pstmt.setInt(3,person.getAge()) ;
			pstmt.setString(4,person.getEmail()) ;
			pstmt.setString(5,person.getId()) ;
			// 进行数据库更新操作
			pstmt.executeUpdate() ;
			pstmt.close() ;
		}
		catch (Exception e)
		{
			throw new Exception("操作出现异常") ;
		}
		finally
		{
			// 关闭数据库连接
			dbc.close() ;
		}
	}
	// 删除操作
	public void delete(String id) throws Exception
	{
		String sql = "DELETE FROM person WHERE id=?" ;
		PreparedStatement pstmt = null ;
		DataBaseConnection dbc = null ;

		// 下面是针对数据库的具体操作
		try
		{
			// 连接数据库
			dbc = new DataBaseConnection() ;
			pstmt = dbc.getConnection().prepareStatement(sql) ;			
			pstmt.setString(1,id) ;
			// 进行数据库更新操作
			pstmt.executeUpdate() ;
			pstmt.close() ;
		}
		catch (Exception e)
		{
			throw new Exception("操作出现异常") ;
		}
		finally
		{
			// 关闭数据库连接
			dbc.close() ;
		}
	}
	// 按ID查询操作
	public Person queryById(String id) throws Exception
	{
		Person person = null ;
		String sql = "SELECT id,name,password,age,email FROM person WHERE id=?" ;
		PreparedStatement pstmt = null ;
		DataBaseConnection dbc = null ;

		// 下面是针对数据库的具体操作
		try
		{
			// 连接数据库
			dbc = new DataBaseConnection() ;
			pstmt = dbc.getConnection().prepareStatement(sql) ;			
			pstmt.setString(1,id) ;
			// 进行数据库查询操作
			ResultSet rs = pstmt.executeQuery() ;
			if(rs.next())
			{
				// 查询出内容,之后将查询出的内容赋值给person对象
				person = new Person() ;
				person.setId(rs.getString(1)) ;
				person.setName(rs.getString(2)) ;
				person.setPassword(rs.getString(3)) ;
				person.setAge(rs.getInt(4)) ;
				person.setEmail(rs.getString(5)) ;
			}
			rs.close() ;
			pstmt.close() ;
		}
		catch (Exception e)
		{
			throw new Exception("操作出现异常") ;
		}
		finally
		{
			// 关闭数据库连接
			dbc.close() ;
		}
		return person ;
	}
	// 查询全部
	public List queryAll() throws Exception
	{
		List all = new ArrayList() ;
		String sql = "SELECT id,name,password,age,email FROM person" ;
		PreparedStatement pstmt = null ;
		DataBaseConnection dbc = null ;

		// 下面是针对数据库的具体操作
		try
		{
			// 连接数据库
			dbc = new DataBaseConnection() ;
			pstmt = dbc.getConnection().prepareStatement(sql) ;			
			// 进行数据库查询操作
			ResultSet rs = pstmt.executeQuery() ;
			while(rs.next())
			{
				// 查询出内容,之后将查询出的内容赋值给person对象
				Person person = new Person() ;
				person.setId(rs.getString(1)) ;
				person.setName(rs.getString(2)) ;
				person.setPassword(rs.getString(3)) ;
				person.setAge(rs.getInt(4)) ;
				person.setEmail(rs.getString(5)) ;

				// 将查询出来的数据加入到List对象之中
				all.add(person) ;
			}
			rs.close() ;
			pstmt.close() ;
		}
		catch (Exception e)
		{
			throw new Exception("操作出现异常") ;
		}
		finally
		{
			// 关闭数据库连接
			dbc.close() ;
		}
		return all ;
	}
	// 模糊查询
	public List queryByLike(String cond) throws Exception
	{
		List all = new ArrayList() ;
		String sql = "SELECT id,name,password,age,email FROM person WHERE name LIKE ? or email LIKE ?" ;
		PreparedStatement pstmt = null ;
		DataBaseConnection dbc = null ;

		// 下面是针对数据库的具体操作
		try
		{
			// 连接数据库
			dbc = new DataBaseConnection() ;
			pstmt = dbc.getConnection().prepareStatement(sql) ;	
			// 设置模糊查询条件
			pstmt.setString(1,"%"+cond+"%") ;
			pstmt.setString(2,"%"+cond+"%") ;
			// 进行数据库查询操作
			ResultSet rs = pstmt.executeQuery() ;
			while(rs.next())
			{
				// 查询出内容,之后将查询出的内容赋值给person对象
				Person person = new Person() ;
				person.setId(rs.getString(1)) ;
				person.setName(rs.getString(2)) ;
				person.setPassword(rs.getString(3)) ;
				person.setAge(rs.getInt(4)) ;
				person.setEmail(rs.getString(5)) ;

				// 将查询出来的数据加入到List对象之中
				all.add(person) ;
			}
			rs.close() ;
			pstmt.close() ;
		}
		catch (Exception e)
		{
			throw new Exception("操作出现异常") ;
		}
		finally
		{
			// 关闭数据库连接
			dbc.close() ;
		}
		return all ;
	}
};
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics