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

📄 jdbconnection.java

📁 有待完善
💻 JAVA
字号:
package DataBase;
import java.sql.*;
public class jdbconnection {
	private final String dbDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
	private final String url="jdbc:microsoft:sqlserver://localhost:1433/MyCD";
	private final String userName="sa";
	private final String password="";
	private Connection con=null;
	private Statement stmt=null;
	private ResultSet rs=null;
	
    public boolean creatConnection(){
    	try{
    		Class.forName(dbDriver).newInstance();
    		con=DriverManager.getConnection(url, userName, password);
    	}
    	catch(SQLException e){
    		System.out.println(e.getMessage());
    	}
    	catch(ClassNotFoundException ex){
    		/**@todo Handle this exception*/;
    	}
    	catch(IllegalAccessException ex){
    		/**@todo Handle this exception*/;
    	}
    	catch(InstantiationException ex){
    		/**@todo Handle this exception*/;
    	}
    	return true;
    }
    
    public boolean executeUpdate(String sql){
    	if(con==null){
    		creatConnection();
    	}
    	try{
    		stmt=con.createStatement();
    		stmt.executeUpdate(sql);
    	}
    	catch(SQLException e){
    		System.out.println(e.getMessage());
    	}
    	return true;
    }
    
	public ResultSet executeQuery(String sql){
    	try{
    		if(con==null){
    			creatConnection();
    		}
    		stmt=con.createStatement();
    		try{
    			rs=stmt.executeQuery(sql);
    		}
    		catch(SQLException e){
    			return null;
    		}
    	}
    	catch(SQLException e){
    		System.out.println(e.getMessage());
    		return null;
    	}
    	return rs;
    }
	
    public boolean commit(){
    	try{
    		con.commit();
    	}
    	catch(SQLException e){
    		System.out.println(e.getMessage());
    	}
    	return true;
    }
    
    public void closeDBConnection(){
    	if(con!=null){
    		try{
    			con.close();
    		}
    		catch(SQLException e){
    			e.printStackTrace();
        		System.out.println(e.getMessage());
        	}
    		finally{
    			con=null;
    		}
    	}
    }
}

⌨️ 快捷键说明

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