package com.fh.controller.ruitai.util;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8
9 //连接瑞泰餐饮前台数据库
10 public class DBConnectSS {
11
12 private static String dbURL = "192.168.186.128"
;
13 private static String databaseName = "ssycyv8"
;
14 private static String dbUser = "sa"
;
15 private static String dbPassword = "88888888"
;
16
17 public static Connection getConnectionForSS() {
18 Connection conn =
null;
19 try {
20 String url = "jdbc:sqlserver://" + dbURL + ";databaseName="
21 + databaseName + ";user=" + dbUser + ";password="
22 + dbPassword;
// sa身份连接
23 // Establish the connection.
24 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"
);
25 conn =
DriverManager.getConnection(url, dbUser, dbPassword);
26 }
catch (ClassNotFoundException |
SQLException e) {
27 e.printStackTrace();
28 }
29 return conn;
30 }
31
32 public static Connection getConnectionForSS(String dbURL,String databaseName,String dbUser,String dbPassword) {
33 Connection conn =
null;
34 try {
35
36 String url = "jdbc:sqlserver://" + dbURL + ";databaseName="
37 + databaseName + ";user=" + dbUser + ";password="
38 + dbPassword;
// sa身份连接
39 // Establish the connection.
40 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"
);
41 conn =
DriverManager.getConnection(url, dbUser, dbPassword);
42 }
catch (ClassNotFoundException |
SQLException e) {
43 e.printStackTrace();
44 }
45 return conn;
46 }
47
48 /**
49 * @param dbtype 数据库类型
50 * @param username 用户名
51 * @param password 密码
52 * @param dburl 数据库连接地址:端口
53 * @param databaseName 数据库名
54 * @return
55 * @throws SQLException
56 * @throws ClassNotFoundException
57 */
58 public static Connection getCon(String dbtype,String username,String password,String dburl,String databaseName)
throws SQLException, ClassNotFoundException{
59 System.out.print("数据库连接地址:"+"jdbc:sqlserver://"+dburl+"; DatabaseName="+databaseName+username+
password);
60 if("mysql"
.equals(dbtype)){
61 Class.forName("com.mysql.jdbc.Driver"
);
62 return DriverManager.getConnection("jdbc:mysql://"+dburl+"/"+databaseName+"?user="+username+"&password="+
password);
63 }
else if("oracle"
.equals(dbtype)){
64 Class.forName("oracle.jdbc.driver.OracleDriver"
);
65 return DriverManager.getConnection("jdbc:oracle:thin:@"+dburl+":"+
databaseName, username, password);
66 }
else if("sqlserver"
.equals(dbtype)){
67 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"
);
68 return DriverManager.getConnection("jdbc:sqlserver://"+dburl+"; DatabaseName="+
databaseName, username, password);
69 }
else{
70 return null;
71 }
72 }
73
74 /**
75 * @description 关闭数据库的所有连接
76 * @param con
77 * @param stmt
78 * @param rs
79 */
80 public static void closeAll(Connection con, Statement stmt, ResultSet rs) {
81 if (rs !=
null)
82 try {
83 rs.close();
84 }
catch (Exception e) {
85 }
86 if (stmt !=
null)
87 try {
88 stmt.close();
89 }
catch (Exception e) {
90 }
91 if (con !=
null)
92 try {
93 con.close();
94 }
catch (Exception e) {
95 }
96 }
97
98
99 // 测试
100 public static void main(String[] args) {
101 // Declare the JDBC objects.
102 Connection con =
null;
103 Statement stmt =
null;
104 ResultSet rs =
null;
105 try {
106 con =
getConnectionForSS();
107 String SQL = SqlConst.getOders("",
null);
108 System.out.println(SQL);
109 stmt =
con.createStatement();
110 rs =
stmt.executeQuery(SQL);
111 int i = 1
;
112 while (rs.next()) {
113 i = i + 1
;
114 System.out.println("个数:" +
i);
115 System.out.println(rs.getString(1) + " " + rs.getString(2
));
116 }
117
118 }
catch (Exception e) {
119 e.printStackTrace();
120 }
finally {
121 closeAll(con, stmt, rs);
122 }
123 }
124
125
126
127 public static String getDbURL() {
128 return dbURL;
129 }
130
131 public static void setDbURL(String dbURL) {
132 DBConnectSS.dbURL =
dbURL;
133 }
134
135 public static String getDatabaseName() {
136 return databaseName;
137 }
138
139 public static void setDatabaseName(String databaseName) {
140 DBConnectSS.databaseName =
databaseName;
141 }
142
143 public static String getDbUser() {
144 return dbUser;
145 }
146
147 public static void setDbUser(String dbUser) {
148 DBConnectSS.dbUser =
dbUser;
149 }
150
151 public static String getDbPassword() {
152 return dbPassword;
153 }
154
155 public static void setDbPassword(String dbPassword) {
156 DBConnectSS.dbPassword =
dbPassword;
157 }
158
159
160 }
jdbc 连接各种数据库
标签:amp manage rgs soft 测试 sql etc 密码 private