From 3f52144d43461a4404e508e5b3fcb7d8f759cae8 Mon Sep 17 00:00:00 2001 From: zhai_lw Date: Wed, 9 Jan 2019 09:59:34 +0800 Subject: [PATCH] =?UTF-8?q?1,=20=E8=AE=BE=E7=BD=AEtomcat=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=202=EF=BC=8C=E9=85=8D=E7=BD=AE=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E8=BF=9E=E6=8E=A5=E6=B1=A0=203=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=B8=80=E7=B1=BBLastProcess=204=EF=BC=8C=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E7=9B=AE=E5=BD=95=E7=BB=93=E6=9E=84=205=EF=BC=8C?= =?UTF-8?q?=E5=85=B6=E4=BB=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GDMS.iml | 1 - src/dao/DBManagement.java | 57 +++++++++++++++++++++++++++++++++------ src/init/Init.java | 2 +- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/GDMS.iml b/GDMS.iml index 4bde68d..4c713fc 100644 --- a/GDMS.iml +++ b/GDMS.iml @@ -29,6 +29,5 @@ - \ No newline at end of file diff --git a/src/dao/DBManagement.java b/src/dao/DBManagement.java index e597616..d8718a4 100644 --- a/src/dao/DBManagement.java +++ b/src/dao/DBManagement.java @@ -1,10 +1,6 @@ package dao; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.Properties; +import java.sql.*; import org.apache.tomcat.jdbc.pool.DataSource; import org.apache.tomcat.jdbc.pool.PoolProperties; @@ -19,20 +15,24 @@ public class DBManagement { public static DataSource dataSource = new DataSource(); - public void init(){ + private static boolean ifInit = false; + + public static void init(){ PoolProperties poolProperties = new PoolProperties(); poolProperties.setUrl(url); poolProperties.setDriverClassName(driverClassName); poolProperties.setUsername(username); poolProperties.setPassword(password); dataSource.setPoolProperties(poolProperties); - } - public static Connection getConnection(){ try { Class.forName(driverClassName); } catch (ClassNotFoundException e) { e.printStackTrace(); } + ifInit = true; + } + public static Connection getConnection(){ + if(!ifInit) return null; try { return DriverManager.getConnection(url,username,password); } catch (SQLException e) { @@ -40,4 +40,45 @@ public class DBManagement { } return null; } + + public static ResultSet query(String sql) { + if(!ifInit) return null; + ResultSet rs = null; + Connection con = null; + try{ + con = DBManagement.getConnection(); + Statement stmt = con.createStatement(); + rs = stmt.executeQuery(sql); + } catch (SQLException e) { + e.printStackTrace(); + }finally { + if(con!=null) { + try { + con.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + return rs; + } + public static void update(String sql) { + if(!ifInit) return; + Connection con = null; + try{ + con = DBManagement.getConnection(); + Statement stmt = con.createStatement(); + stmt.executeUpdate(sql); + } catch (SQLException e) { + e.printStackTrace(); + }finally { + if(con!=null) { + try { + con.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + } } diff --git a/src/init/Init.java b/src/init/Init.java index dfc60d4..3d42bad 100644 --- a/src/init/Init.java +++ b/src/init/Init.java @@ -13,7 +13,7 @@ import java.io.IOException; @WebServlet(name = "Init") public class Init extends HttpServlet { public void init(){ - new DBManagement().init(); + DBManagement.init(); new test(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {