dbconnection.java

来自「A brew application taking backup of the 」· Java 代码 · 共 75 行

JAVA
75
字号
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package DBConnection;import Login.SessionUtil;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.logging.Level;import java.util.logging.Logger;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.servlet.http.HttpSession;import javax.sql.DataSource;public class DBConnection {    Connection conn = null;    Statement stmt = null;    InitialContext ctx;    DataSource ds;    ResultSet rs;    HttpSession session = SessionUtil.getSession();    public Connection Mobile_Master() throws SQLException {                   try {                if(session.getAttribute("connection").toString().equals("")) {                    System.out.println("Getting Connection..");                    ctx = new InitialContext();                    ds = (DataSource) ctx.lookup("Mobile_Master");                    conn = ds.getConnection();                    session.setAttribute("connection", conn);                } else{                    conn = (Connection) session.getAttribute("connection");                }            } catch (NamingException ex) {                Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);            }                   return conn;    }    public void insert(String Query) throws SQLException {        if (conn==null)            conn = Mobile_Master();        try {            stmt = conn.createStatement();            stmt.execute(Query);        } catch (SQLException ex) {            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);        }          }    public void delete(String Query) throws SQLException {        if (conn==null)           conn = Mobile_Master();        insert(Query);          }    public ResultSet select(String Query) throws SQLException {        if (conn==null)            conn = Mobile_Master();        try {            stmt = conn.createStatement();            stmt.execute(Query);            rs = stmt.getResultSet();        } catch (SQLException ex) {            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);        }              return rs;    }}

⌨️ 快捷键说明

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