Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package db;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Properties;
- public class DBConnection {
- private static DBConnection dbconnection;
- private Connection connection;
- private static final String dbName = "School";
- private static final String serverAdress = "localhost";
- private static final int serverPort = 1433;
- private DBConnection() {
- String connectionString = String.format("jdbc:sqlserver://%s:%d;databaseName=%s;user=%s;password=%s",serverAdress,serverPort,dbName);
- Properties connectionProps = getConnectionProperties();
- try {
- connection = DriverManager.getConnection(connectionString,connectionProps);
- } catch (SQLException e) {
- System.err.println("Could not connect to databse " + dbName);
- e.printStackTrace();
- }
- }
- public static DBConnection getInstance() {
- if (dbconnection == null) {
- dbconnection = new DBConnection();
- }
- return dbconnection;
- }
- public Connection getConnection(){
- return connection;
- }
- private Properties getConnectionProperties(){
- Properties connectionProps = new Properties();
- connectionProps.put("user","sa");
- connectionProps.put("password", "secret");
- return connectionProps;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement