conn.java

来自「jsp版图书管理系统」· Java 代码 · 共 71 行

JAVA
71
字号
package com.ww.j2ee;

import java.sql.*;
public class Conn{
	private static Connection con;
	private ResultSet rs;
	private Statement st;
	private static final String drivername="com.mysql.jdbc.Driver";
	private static final String url="jdbc:mysql://localhost:3306/j2ee_booksys?useUnicode=true&characterEncoding=GB2312";
	private static final String user="root";
	private static final String password="root";
	
	public static synchronized Connection getCon() throws Exception{
		try{
			Class.forName(drivername);
			con=DriverManager.getConnection(url,user,password);
			return con;
		}catch(Exception e){
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	public Statement getSt(){
		try{
			con=getCon();
			st=con.createStatement();
		    return st;	
		}catch(Exception e){
			System.err.println(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	
	public ResultSet getRs(String sql){
		try{
			st=getSt();
			rs=st.executeQuery(sql);
		    return rs;	
		}catch(Exception e){
			System.err.println(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	
	public synchronized void close(){
		try{
			if(st!=null){
				st.close();
				st=null;
			}
		}catch(Exception e){
			System.err.println(e.getMessage());
			e.printStackTrace();
		}
		
		try{
			if(con!=null){
				con.close();
				con=null;
			}
		}catch(Exception e){
			System.err.println(e.getMessage());
			e.printStackTrace();
		}
	}
	
	
}

⌨️ 快捷键说明

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