add.java

来自「改代码是采用JSP实现的在线办公自动化管理系统」· Java 代码 · 共 62 行

JAVA
62
字号
package com.zh.conpool;
import javax.naming.*;
import javax.sql.DataSource;
import java.sql.*;
import java.util.Date;
public class Condata{
	private static Connection con;
	private static Context ctx;
	private static Context env;
	Date date=new Date();
	
	public static synchronized Connection getConnection() throws Exception{
		try{
			ctx=new InitialContext();
			env=(Context)ctx.lookup("java:comp/env");
			DataSource ds=(DataSource)env.lookup("jdbc/sa");
			return con=ds.getConnection();
		}
		catch(SQLException e){
			throw e;
		}
		catch(NamingException e){
			throw e;
		}

	}
	public ResultSet executeQuery(String sql){
		try{
			Statement stmt=con.createStatement();
			rs=stmt.executeQuery(sql);
		}catch(SQLException er){
			System.err.print(er.getMessage());
		}
			return rs;
		}
	public int executeUpdate(String sql){
		int result=0;
		try{
			Statement stmt=con.createStatement();
			result=stmt.executeUpdate(sql); 
		}catch(SQLException eer){
			System.out.print(eer.getMessage());
		}
		return result;
	}
	public void close(){
		try{
			if(con!=null){
				con.close();
			}
		}catch(Exception err){
			System.out.print(err);
		}
		try{
			if(rs!=null){
				rs.close();
			}
		}catch(Exception err){
			System.out.print(err);
		}
	}
}

⌨️ 快捷键说明

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