connectdb.java

来自「酒店管理系统」· Java 代码 · 共 129 行

JAVA
129
字号
package database;
import java.sql.*;
import javax.naming.InitialContext;
//import javax.sql.DataSource;

public class connectDB {
	private Connection con = null;
	private Statement st = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	/*private void init(){
		try{
			//InitialContext ctx = new InitialContext();
			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
		    Connection con= DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=hotel","sa","");
			 // return conn;

		}catch(Exception e){
			e.printStackTrace();
		}
	}*/
	public connectDB(){
		try{
			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
		    Connection con= DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=hotel","sa","");
			 // return conn;
			st = con.createStatement();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void excuteQuery(String s){
		try{
			if(st != null)
				rs = st.executeQuery(s);
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	public int excuteUpdate(String s){
		int status = 0;
		try{
			if(st != null)
				status = st.executeUpdate(s);
		}catch(Exception e){
			e.printStackTrace();
		}
		return status;
	}
	
	public void setString(int i,String s){
		try{pstmt.setString(i,s);}
		catch(Exception e){e.printStackTrace();}
	}
	public String getString(int i)throws Exception{
		return rs.getString(i);
	}
	public String getString(String s)throws Exception{
		return rs.getString(s);
	}
	
	public void setDouble(int i, double d){
		try{pstmt.setDouble(i, d);}
		catch(Exception e){e.printStackTrace();}
	}
	public double getDouble(int i) throws Exception{
		return rs.getDouble(i);
	}
	public double getDouble(String s) throws Exception{
		return rs.getDouble(s);
	}
	
	public void setInt(int i,int j){
		try{pstmt.setInt(i, j);}
		catch(Exception e){e.printStackTrace();}
	}
	public int getInt(int i)throws Exception{
		return rs.getInt(i);
	}
	public int getInt(String s)throws Exception{
		return rs.getInt(s);
	}
	
	public void setDate(int i,Date date){
		try{pstmt.setDate(i, date);}
		catch(Exception e){e.printStackTrace();}
	}
	public Date getDate(int i)throws Exception{
		return rs.getDate(i);
	}
	public Date getDate(String s)throws Exception{
		return rs.getDate(s);
	}
	
	public void setTime(int i,Time time){
		try{pstmt.setTime(i, time);}
		catch(Exception e){e.printStackTrace();}
	}
	public Time getTime(int i)throws Exception{
		return rs.getTime(i);
	}
	public Time getTime(String s)throws Exception{
		return rs.getTime(s);
	}
	public boolean next(){
		try{return rs.next();}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public void close(){
		try{
			if(con != null)
				con.close();
			if(st != null)
				st.close();
			if(rs != null)
				rs.close();
		}
		catch(Exception e){e.printStackTrace();}
	}
}


⌨️ 快捷键说明

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