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

📄 database.java

📁 图书管管理系统是基于JAVA的 很好的
💻 JAVA
字号:
package h6.action;import java.sql.*;/** * 数据库最底层的操作,提供查找,删除和更新等基本操作 * @author lee * @version 0.5 *  * */public class DataBase {	private SystemParameter conf = new SystemParameter();	private String driverName = conf.getDateBaseDriver();	private String userName = conf.getDateBaseUser();	private String userPasswd = conf.getDateBasePassword();	private String dbName= conf.getDateBaseName();	private String url = conf.getDateBaseUrl()+dbName;	private Connection connection;	private Statement statement;  	public DataBase(){		try{			Class.forName(driverName );		}catch ( ClassNotFoundException cnfex ) {			  System.err.println( "装载 JDBC/ODBC 驱动程序失败。" );			  cnfex.printStackTrace();			  System.exit( 1 ); // terminate program		  }	}	public String getUserName(){		return userName;	}		public String getDbName(){		return dbName;	}		public String getPassWord(String pass){		return userPasswd;	}		/**		 *	从表中查找,返回查找结果的记录集ResultSet	 * 	@param table	要查找的表的名字	 * 	@param attr		要查找的表中的所有属性安查找顺序放入此数组			 * 	@param where	sql语句中条件部分,即where部分	 * 	@return 	从数据库查找得到的ResultSet	 *  例:	select name,age from student where nation='china'	 *  	String table = "student";	 *  	String attr[] = {"name","age"};	 *  	String where = "nation='china'";	 */	public ResultSet search(String table,String[] attr,String where){		int i = 0,j = attr.length;		ResultSet rs = null;		getConnection();		try{			  statement = connection.createStatement();			  String sql="SELECT ";			  for(i=0;i < j;i++){				  if(i == 0)					  sql += attr[i];				  else					  sql += ","+attr[i];			  }			  			  if(where.length()>0)				  sql += " FROM "+table+" where "+where;			  else				  sql += " FROM "+table;			  			  			  rs = statement.executeQuery(sql); 			  			  			  return rs;		  }catch(Exception e){			  System.out.println("查找数据时出错!");			  System.out.println(e);			  closeConnection();			  return null;		  }	}		/**	 * 从表中删除相应的记录,删除成功则返回true,否则返回false	 * @param table		要进行删除操作的表的名字			 * @param where		sql语句中条件部分,即where部分	 * @return		执行操作成功与否,成功则返回true,否则返回false	 * 例:	delete from student where age=20	 * 		String table = "student";	 * 		String where = "age=20";	 */	public boolean delete(String table,String where){		String sql = "DELETE FROM " + table + " where " + where;		getConnection();		try{			statement = connection.createStatement();			statement.execute(sql);		}catch(Exception e){			System.out.println("删除数据库里数据里出错!");			System.out.println(e);			closeConnection();			return false;		}		closeConnection();		return true;	}	/**	 * 插入相应的记录到表中,操作成功则返回true,否则返回false	 * @param table		要进行插入操作的表的名字			 * @param value		插入各属性的值	 * @return		执行操作成功与否,成功则返回true,否则返回false	 * 例:	insert into student values ('小明',20,‘男’)	 * 		String table = "student";	 * 		String value = "'小明',20,‘男’";	 */	public boolean insert(String table,String value){		String sql = "INSERT INTO " + table + " VALUES (" + value + ") ";		System.out.println(sql);		getConnection();		try{			statement = connection.createStatement();			statement.execute("SET NAMES 'GBK'");			statement.execute(sql);		}catch(Exception e){			System.out.println("往数据库里插入数据时出错!");			System.out.println(e);			closeConnection();			return false;		}		closeConnection();		return true;	}		/**	 * 更新表中的记录,操作成功则返回true,否则返回false	 * @param table			要进行更新操作的表的名字			 * @param setValue		更新的相应属性经及更新后的值	 * @param where			sql语句中条件部分,即where部分	 * @return		执行操作成功与否,成功则返回true,否则返回false	 * 例:	update student set name='大明',age=22 where name='小明'	 * 		String table = "student";	 * 		String setValue = "name='大明',age=22“;	 * 		String where = "name='小明'";	 */	public boolean update (String table,String setValue,String where){		String sql = "UPDATE " + table + " SET " + setValue;		getConnection();		if (where.length() > 0){			sql += " WHERE " + where;		}				try{			statement = connection.createStatement();			statement.execute("SET NAMES 'GBK'");			statement.execute(sql);		}catch(Exception e){			System.out.println("更新数据库数据时出错!");			System.out.println(e);			closeConnection();			return false;		}		closeConnection();		return true;	}		 private void getConnection(){//取得与数据库的连接		  try {			  connection = DriverManager.getConnection(			  url, userName, userPasswd );			  }			  //捕获连接数据库异常			  catch ( SQLException sqlex ) {				  System.err.println( "无法连接数据库" );				  sqlex.printStackTrace();				  System.exit( 1 ); // terminate program			  } 	  }	 	 public void closeConnection(){		 try{                        if(null != statement)                            statement.close();                        if(null != connection)                            connection.close();			 statement = null;			 connection = null;		 }catch(Exception e){			 e.printStackTrace();		 }	 }}

⌨️ 快捷键说明

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