-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBUtils.java
More file actions
38 lines (36 loc) · 1.45 KB
/
Copy pathDBUtils.java
File metadata and controls
38 lines (36 loc) · 1.45 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* @author thien
*/
public class DBUtils {
public static Connection getConnection() {
try {
String url = "jdbc:sqlserver://" + serverName + ":" + portNumber + "\\" + instance + ";databaseName=" + dbName;
if (instance == null || instance.trim().isEmpty()) {
url = "jdbc:sqlserver://" + serverName + ":" + portNumber + ";databaseName=" + dbName;
}
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
return DriverManager.getConnection(url, userID, password);
} catch (SQLException ex) {
System.out.println("Connection error! " + ex.getMessage());
} catch (ClassNotFoundException ex) {
System.out.println("Connection error! " + ex.getMessage());
}
return null;
}
private final static String serverName = "localhost";
private final static String dbName = "MaccaShop";
private final static String portNumber = "1433";
private final static String instance = "";//LEAVE THIS ONE EMPTY IF YOUR SQL IS A SINGLE INSTANCE
private final static String userID = "sa";
private final static String password = "123456";
}