lxw
2020-07-11 9db52f2f2dd3665fe9da1ae5657e0167c3a34d40
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
 
package com.fgkj.db;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
import com.fgkj.dto.ServiceModel;
 
public class DBUtil1 {
 
    private static String classDriver="com.mysql.jdbc.Driver";
//    private static String url="jdbc:mysql://192.168.0.34:5306/";
//    private static String ip="192.168.0.34";
    private static String ip="211.149.178.177";
//    private static String ip = "127.0.0.1";
    private static String username = "root";
    private static String password = "lmx8688139";
    
//    private static String url="jdbc:mysql://192.168.0.34:5306/";
//    private static String username="root";
//    private static String password="lmx8688139";
 
    private static String port = ":5307/";
    private static String url = "jdbc:mysql://";
    private static String reConnect="?autoReconnect=true";
//    private static String username="root";
//    private static String password="lmx8688139";
    
    public static Connection getConn(String databaseName){
        Connection conn=null;
        try {
            Class.forName(classDriver);
            DriverManager.setLoginTimeout(10);
            conn = DriverManager.getConnection(url+ip+port+databaseName+reConnect, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    
    public static Connection getConn(){
        Connection conn=null;
        try {
            Class.forName(classDriver);
            DriverManager.setLoginTimeout(10);
            conn = DriverManager.getConnection(url+ip+port+reConnect, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    
    //关闭资源
    public static void close(ResultSet rs,Statement stat,Connection conn){
        try {
            if(rs != null)rs.close();
            if(stat != null)stat.close();
            if(conn != null)conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    
    
    public static String getIp() {
        return ip;
    }
 
    public static ServiceModel setIp(String ip) {
        ServiceModel model = new ServiceModel();
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url + ip + port,username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(conn != null){
            model.setCode(1);
            model.setMsg("修改成功!");
            DBUtil1.ip=ip;
        }else{
            model.setCode(0);
            model.setMsg("修改失败请检查该ip服务器上是否配置了数据库!");
        }
        System.out.println(model);
        return model;
    }
 
    public static void main(String[] args){
        System.out.println(getConn(IDatabaseName.DB_BATT_TESTDATA));
        System.out.println(getConn(IDatabaseName.DB_BATT_TESTDATA));
 
//        Connection conn=getConn(IDatabaseName.DB_BATTINF);
//        try {
//            PreparedStatement ps=conn.prepareStatement("select count(*) from tb_battinf");
//            ResultSet rs=ps.executeQuery();
//            while(rs.next()){
//                int count=rs.getInt(1);
//                System.out.println("查询成功"+count);
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
        
    }
}