Package util;
/ * * Date: zhangyu6050; * Todo: A database package class, the role is: * Get database connection * Select the database by accepting the SQL statement and returns the result * The following judgment is made: 1 If SQL is used to query :, * /// package util;
Import java.util.list; import java.sql. *; public class mysql {
private String dirverName = Configs.dirverName; private String URL = Configs.URL; private String password = Configs.password; private String userName = Configs.userName; private Connection conn = null; private Statement stmt = null; private PreparedStatement prepstmt = null; Public mysql () {Try {getDataSource (); stmt = conn.createstatement ();} catch (system.out.print ("Conn.createStatement () error!"); E.PrintStackTrace (); }}} Public mysql (string query) {init (query);
Protected voidin {try {stmt = conn.createstatement (); prepareStatement (aqurey);} catch (sqlexception e) {system.out.print ("Conn.createStatement () error!"); E. PRINTSTACKTRACE ();
} // This method is to prepare for creating a MySQL object, but I found it best to use a single design!! index, String value) throws SQLException {prepstmt.setString (index, value);} public void prepareStatement (String sql) throws SQLException {prepstmt = conn.prepareStatement (sql);} public void close () throws SQLException {if (prepstmt! = null) prepstmt.close (); if (stmt = null) prepstmt.close ();!} public ResultSet executeQuery () throws SQLException {if (prepstmt = null) {return prepstmt.executeQuery ();!} else return null Public void getDataSource () {Try {class.Forname (DirverName); conn = drivermanager.getConnection (URL, Username, Password);} catch (exception e) {// Error incorporated! System.out.print ("Connection error!"); E.PrintStackTrace ();}}}