cari

Rumah  >  Soal Jawab  >  teks badan

Isu keselamatan benang sambungan java ke mysql

Saya mencari di Internet selama N hari, dan hampir tiada penyelesaian yang selamat untuk thread Masalah yang sama boleh diselesaikan dengan mudah dengan Redis Saya menukarnya kepada kelas alat yang saya temui dalam talian .Idea semasa saya adalah untuk menambah kata kunci yang disegerakkan, tetapi saya masih merasakan bahawa terdapat masalah.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

<code>class MySQLUtil {

   

  private static final String driver = "com.mysql.jdbc.Driver";

  private static final String url = "jdbc:mysql://192.168.31.103:3306/";

  private static final String character = "?useUnicode=true&characterEncoding=utf8";

  private static final String ssl = "&useSSL=false";

  private static final String user = "root";

  private static final String password = "111111";

  private static Connection connection = null;

  private static Statement statement = null;

  private static PreparedStatement ps = null;

  private static ResultSet rs = null;

   

  boolean TestConnection(String db) {

    try {

      Class.forName(driver);

      Connection connection = DriverManager.getConnection(url + db + character + ssl, user, password);

       

      if (!connection.isClosed()) {

        CloseConnection();

        return true;

      }

    } catch (Exception e) {

      e.printStackTrace();

    }

    return false;

  }

   

  synchronized private void ConnectToDB(String db) {

    try {

      Class.forName(driver);

      Connection connection = DriverManager.getConnection(url + db + character + ssl, user, password);

       

      if (!connection.isClosed()) {

        statement = connection.createStatement();

      }

    } catch (Exception e) {

      e.printStackTrace();

    }

  }

   

  synchronized private void CloseConnection() {

    try {

      if (rs != null) {

        rs.close();

      }

    } catch (SQLException e) {

      e.printStackTrace();

    }

     

    try {

      if (ps != null) {

        ps.close();

      }

    } catch (SQLException e) {

      e.printStackTrace();

    }

     

    try {

      if (connection != null) {

        connection.close();

      }

    } catch (SQLException e) {

      e.printStackTrace();

    }

  }

   

  synchronized void ModifyData(String db, String data) {

     

    ConnectToDB(db);

    try {

      statement.execute(data);

    } catch (SQLException e) {

      e.printStackTrace();

    } finally {

      CloseConnection();

    }

     

  }

   

  synchronized List ReadData(String db, String data) {

    List<String> list = new ArrayList<>();

    int count;

    ConnectToDB(db);

     

    try {

      rs = statement.executeQuery(data);

      ResultSetMetaData rsmd;

      rsmd = rs.getMetaData();

      count = rsmd.getColumnCount();

       

      while (rs.next()) {

        for (int i = 1; i <= count; i++) {

          String label = rsmd.getColumnLabel(i);

          list.add(label);

          String value = rs.getString(i);

          list.add(value);

        }

      }

       

    } catch (SQLException e) {

      e.printStackTrace();

    } finally {

      CloseConnection();

    }

    return list;

  }

}</code>

phpcn_u1582phpcn_u15822810 hari yang lalu1174

membalas semua(4)saya akan balas

  • 给我你的怀抱

    给我你的怀抱2017-06-16 09:21:30

    Untuk memastikan data antara sambungan adalah bebas (tidak dikongsi), saya rasa anda mahu melaksanakan Kolam sambungan:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    <code>ComboPooledDataSource cpds = new ComboPooledDataSource();

    cpds.setDriverClass( "org.postgresql.Driver" );

    cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" );

    cpds.setUser("caiyongji");

    cpds.setPassword("test-password");

     

    cpds.setMinPoolSize(5);

    cpds.setAcquireIncrement(5);

    cpds.setMaxPoolSize(20);</code>

    balas
    0
  • 某草草

    某草草2017-06-16 09:21:30

    Selepas sedikit pengubahsuaian, mungkin lebih baik untuk mendengar rakan di atas dan menggunakan kumpulan sambungan pangkalan data yang matang. Tidak perlu mencipta semula roda

    • Gunakan singleton untuk memastikan keunikan sambungan pangkalan data

    • Ubah suai

      penggunaan kata kunci untuk meningkatkan kecekapansynchronized

    • Tambah

      kata kunci untuk meningkatkan kestabilanvolatile

    • 1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      63

      64

      65

      66

      67

      68

      69

      70

      71

      72

      73

      74

      75

      76

      77

      78

      79

      80

      81

      82

      83

      84

      85

      86

      87

      88

      89

      90

      91

      92

      93

      94

      95

      96

      97

      98

      99

      100

      101

      102

      103

      104

      105

      106

      107

      108

      109

      110

      111

      112

      113

      114

      115

      116

      117

      118

      119

      120

      121

      122

      123

      124

      125

      126

      127

      128

      129

      130

      131

      132

      133

      134

      135

      136

      137

      138

      139

      140

      141

      142

      143

      144

      145

      146

      147

      148

      149

      150

      151

      152

      153

      154

      155

      156

      157

      158

      159

      <code>package com.singleton;

       

      import java.sql.Connection;

      import java.sql.DriverManager;

      import java.sql.PreparedStatement;

      import java.sql.ResultSet;

      import java.sql.ResultSetMetaData;

      import java.sql.SQLException;

      import java.sql.Statement;

      import java.util.ArrayList;

      import java.util.HashMap;

      import java.util.List;

      import java.util.Map;

       

      /**

       * <b>功能:</b><br>

       * <br>

       * <b>完整路径:</b> com.singleton.MySQLUtil <br>

       * <b>创建日期:</b> 2017年6月15日 上午10:42:49 <br>

       *

       * @author pfyangf<br>

       * @version 1.0

       */

      class MySQLUtil {

           

          private MySQLUtil(){}

           

          private static volatile Connection connection = null;

       

          private static final String driver = "com.mysql.jdbc.Driver";

          private static final String url = "jdbc:mysql://192.168.31.103:3306/";

          private static final String character = "?useUnicode=true&characterEncoding=utf8";

          private static final String ssl = "&useSSL=false";

          private static final String user = "axtest";

          private static final String password = "axtest123";

          private static Statement statement = null;

          private static PreparedStatement ps = null;

          private static ResultSet rs = null;

           

          public static void main(String[] args) {

              /*Connection newConnection;

              try {

                  newConnection = MySQLUtil.connectToDB("xxx");

                  System.out.println(newConnection.isClosed());

              } catch (Exception e) {

                  //TODO 异常处理

                  e.printStackTrace();

              }*/

              try {

                  List<Map<String, Object>> data = MySQLUtil.readData("xxx", "select now() from dual");

                  System.out.println(data.toString());

              } catch (Exception e) {

                  e.printStackTrace();

              }

          }

       

          boolean TestConnection(String db) {

              try {

                  Class.forName(driver);

                  Connection connection = DriverManager.getConnection(url + db + character + ssl, user, password);

       

                  if (!connection.isClosed()) {

                      CloseConnection();

                      return true;

                  }

              } catch (Exception e) {

                  e.printStackTrace();

              }

              return false;

          }

       

          /**

           * <b>功能:获取DB连接</b><br>

           * <br>

           * @Author:pfyangf , 2017年6月15日

           * @param db

           * @return

           * @throws Exception Connection

           **/

          public static Connection connectToDB(String db) throws Exception {

              if(null == connection){

                  synchronized (MySQLUtil.class) {

                      if(null == connection){

                          Class.forName(driver);

                          connection = DriverManager.getConnection(url + db + character + ssl, user, password);

                          statement = connection.createStatement();

                      }

                  }

              }

              return connection;

          }

       

          private static void CloseConnection() {

              try {

                  if (rs != null) {

                      rs.close();

                  }

              } catch (SQLException e) {

                  e.printStackTrace();

              }

       

              try {

                  if (ps != null) {

                      ps.close();

                  }

              } catch (SQLException e) {

                  e.printStackTrace();

              }

       

              try {

                  if (connection != null) {

                      connection.close();

                  }

              } catch (SQLException e) {

                  e.printStackTrace();

              }

          }

       

          public static void ModifyData(String db, String data) throws Exception {

       

              connectToDB(db);

              try {

                  statement.execute(data);

              } catch (SQLException e) {

                  e.printStackTrace();

              } finally {

                  CloseConnection();

              }

       

          }

       

          public static List<Map<String, Object>> readData(String db, String sql) throws Exception {

              List<Map<String, Object>> list = new ArrayList<>();

              int count;

              connectToDB(db);

       

              try {

                  rs = statement.executeQuery(sql);

                  ResultSetMetaData rsmd;

                  rsmd = rs.getMetaData();

                  count = rsmd.getColumnCount();

       

                  while (rs.next()) {

                      Map<String, Object> map = null;

                      for (int i = 1; i <= count; i++) {

                          map = new HashMap<>();

                          map.put(rsmd.getColumnLabel(i), rs.getString(i));

                          list.add(map);

                      }

                  }

       

              } catch (SQLException e) {

                  e.printStackTrace();

              } finally {

                  CloseConnection();

              }

              return list;

          }

      }</code>

      balas
      0
  • 给我你的怀抱

    给我你的怀抱2017-06-16 09:21:30

    Tidak perlu menyegerakkan, berbilang sambungan tidak penting.
    Pangkalan data mempunyai kuncinya sendiri.
    Anda juga boleh menggunakan kolam sambungan secara terus.

    balas
    0
  • 滿天的星座

    滿天的星座2017-06-16 09:21:30

    Terima kasih atas jawapan anda Saya menukar sedikit kod. Tolong bantu saya melihat jika ada masalah pembolehubah ahli Semua parameter dan Nilai pulangan diluluskan dan semua pembolehubah diisytiharkan dalam kaedah

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    90

    91

    92

    93

    94

    95

    96

    97

    98

    99

    100

    101

    102

    103

    104

    105

    106

    107

    108

    109

    110

    <code>class MySQLUtil {

         

        private static final String driver = "com.mysql.jdbc.Driver";

        private static final String url = "jdbc:mysql://192.168.31.103:3306/";

        private static final String character = "?useUnicode=true&characterEncoding=utf8";

        private static final String ssl = "&useSSL=false";

        private static final String user = "root";

        private static final String password = "111111";

         

        boolean TestConnection(String db) {

            try {

                Class.forName(driver);

                Connection connection = DriverManager.getConnection(url + db + character + ssl, user, password);

                 

                if (!connection.isClosed()) {

                    CloseConnection(connection, null);

                    return true;

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

            return false;

        }

         

        private List ConnectToDB(String db) {

             

            List<Object> list = new ArrayList<>();

             

            try {

                Class.forName(driver);

                Connection connection = DriverManager.getConnection(url + db + character + ssl, user, password);

                 

                if (!connection.isClosed()) {

                    Statement statement = connection.createStatement();

                    list.add(1, connection);

                    list.add(2, statement);

                    return list;

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

             

            return list;

        }

         

        private void CloseConnection(Connection connection, ResultSet rs) {

            try {

                if (rs != null) {

                    rs.close();

                }

            } catch (SQLException e) {

                e.printStackTrace();

            }

             

            try {

                if (connection != null) {

                    connection.close();

                }

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

         

        public void ModifyData(String db, String data) {

             

            List list = ConnectToDB(db);

            Connection connection = (Connection) list.get(1);

            Statement statement = (Statement) list.get(2);

             

            try {

                statement.execute(data);

            } catch (SQLException e) {

                e.printStackTrace();

            } finally {

                CloseConnection(connection, null);

            }

             

        }

         

        public List ReadData(String db, String data) {

            List<String> result = new ArrayList<>();

            ResultSet rs = null;

            int count;

             

            List list1 = ConnectToDB(db);

            Connection connection = (Connection) list1.get(1);

            Statement statement = (Statement) list1.get(2);

             

            try {

                rs = statement.executeQuery(data);

                ResultSetMetaData rsmd;

                rsmd = rs.getMetaData();

                count = rsmd.getColumnCount();

                 

                while (rs.next()) {

                    for (int i = 1; i <= count; i++) {

                        String label = rsmd.getColumnLabel(i);

                        result.add(label);

                        String value = rs.getString(i);

                        result.add(value);

                    }

                }

            } catch (SQLException e) {

                e.printStackTrace();

            } finally {

                CloseConnection(connection, rs);

            }

            return result;

        }

    }</code>

    balas
    0
  • Batalbalas