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

📄 dbexcute.java

📁 重点不是信息管理系统
💻 JAVA
字号:
package com.yijia_ctgu.DB;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Vector;

import org.apache.commons.beanutils.RowSetDynaClass;

import com.yijia_ctgu.DB.DBConnect;
import com.yijia_ctgu.exception.NotQueryException;
/**
 * 
 * @author yijia
 *
 */
public class DBExcute{
	Connection conn;
	String condition;
	Statement stm;
	ResultSet rs;
	public DBExcute(){}
	public void initize(){
		 try{
			 if(conn==null){
				 conn=DBConnect.getConnection();
			 }
			 
			 if(stm==null){
				 stm=conn.createStatement();
			 }
		 }
		 catch(SQLException ex){
			 System.out.println("Initialization error");
		 }
	}
	 public void close(){
		 try{
			 if(rs!=null) rs.close();
			 if(stm!=null)stm.close();
			 if(conn!=null)conn.close();
		 }catch(SQLException ex){
			 System.out.println("close error");
		 }
	 }
	 
	 public String queryString(int i,String sql)throws SQLException,NotQueryException{
		 initize();
		 try{
			 String str=null;
			 rs=stm.executeQuery(sql);
			 //System.out.println("1");
			 if(rs.next()){
				 str=rs.getString(i);
				 return str;
			 }
			 else {
				 NotQueryException cex=new NotQueryException();
				 throw cex;
			 }
		 }catch(SQLException ex){
			 System.out.println("throws SQLException int the mothod:queryString of DBExcute");
			 throw ex;
		 }catch(NotQueryException cex){
			 System.out.println("throws NotQueryException int the mothod:queryString of DBExcute");
			 throw cex;
		 }     
		 }
	 public String queryString(String sql) throws SQLException,NotQueryException{
		 return queryString(1,sql);
	 }
	 public List getTableNames(){
		 initize();
		 String sql="show tables";
		 List list=new Vector();
		 try{
			 rs=stm.executeQuery(sql);
			 //System.out.println(1);
			 while(rs.next()){
				 list.add(rs.getString(1));
			 }
			 return list;
		 }catch(SQLException ex){
			 System.out.println("throws SQLException int the mothod:getTableNames of DBExcute");
			 return null;
		 }
	 }
	 public List queryList(String sql)throws SQLException, NotQueryException{
		 initize();
		 try{
			 rs=stm.executeQuery(sql);
			 RowSetDynaClass rsdc = new RowSetDynaClass(rs);
			 List list = rsdc.getRows();
			 if(list==null){
				 throw new NotQueryException();
			 }
			 else return list;
		 }catch(SQLException ex){
			 System.out.println("throws SQLException int the mothod:queryList of DBExcute");
			 throw ex;
		 }
 }
	 public boolean update(String sql)throws SQLException{
		 initize();
		 try{
			 stm.executeUpdate(sql);
			 return true;
		 }catch(SQLException ex){
			 System.out.println("throws SQLException int the mothod:update of DBExcute");
			 throw ex;
		 }
	 }
	 
}

⌨️ 快捷键说明

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