dbconnbean.java

来自「用Jsp实现的图书管理系统代码」· Java 代码 · 共 135 行

JAVA
135
字号

package conndb;

import java.sql.*;

public class DbConnBean
{
	private static Connection conn;
	private Statement stmt;
	private ResultSet rs;
	/*********************************************
	**函数功能:创建数据库Statement
	**********************************************/
	public void init(Connection conn){
		try
		{
			stmt=this.conn.createStatement();
		}
		catch (Exception e)
		{
			System.out.println("init:"+e.getMessage());
		}
	}
	/*********************************************
	**函数功能:创建数据库Statement
	**********************************************/
	public void close(){
		try
		{
			if (rs!=null)
			{
				rs.close();
			}
		}
		catch (Exception e)
		{
			System.out.println("rs.close():"+e.getMessage());
		}
		try
		{
			if (stmt!=null)
			{
				stmt.close();
			}
		}
		catch (Exception e)
		{
			System.out.println("stmt.close():"+e.getMessage());
		}
		try
		{
			if (conn!=null)
			{
				conn.close();
			}
		}
		catch (Exception e)
		{
			System.out.println("conn.close():"+e.getMessage());
		}
		}

	/*********************************************
	**函数功能:查询数据
	**********************************************/
	public int select(String sql){
		int k=-10;
		this.init(this.getConn());
		try
		{
			k=0;
			rs=stmt.executeQuery(sql);
			if (rs.next())
			{
				k=k+1;
			}
		}
		catch (Exception e)
		{
			k=-1;
			System.out.println("select():"+e.getMessage());
			this.close();
		}
		this.close();
		return k;
		}
	/*********************************************
	**函数功能:更新\删除\插入 数据
	**********************************************/
	public int update(String sql){
		int k=-10;
		this.init(this.getConn());
		try
		{
			k=0;
			k=stmt.executeUpdate(sql);
		}
		catch (Exception e)
		{
			k=-1;
			System.out.println("update():"+e.getMessage());
		}
		this.close();
		return k;
		}
	/*********************************************
	**函数功能:创建数据库连接
	**********************************************/
	public static synchronized Connection getConn(){
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			conn=DriverManager.getConnection("jdbc:odbc:book","","");//需要配置数据源,access和sql server2000都可以连接的
		}
		catch (Exception e)
		{
			System.out.println("getConn():"+e.getMessage());
		}
		return conn;
		}
	/*********************************************
	**函数功能:做编码转换,防止中文难乱
	**********************************************/	
	public String chStr(String str){
		try
		{
			byte[] temp=str.getBytes("ISO8859-1");
			String temp2=new String(temp);
			return temp2; 	
		}catch(Exception e){System.out.println("chStr():"+e.getMessage());}
		return "null";				
		}
	

}

⌨️ 快捷键说明

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