Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Database {
- private String user;
- private String database;
- private String password;
- private String port;
- private String hostname;
- private Connection connection;
- public void setup(){
- try{
- // Tak jak mówiłem, te wartości NIE powinny być bezpośrednio w kodzie.
- hostname = "localhost";
- port = "3306"; //3306 to domyślny port
- database = "testdb";
- user = "szymi";
- password = "czacha Ci dymi";
- createTestTable();
- } catch(Exception e){
- e.printStackTrace();
- }
- }
- public Connection getConnection() {
- try{
- if (connection != null && !connection.isClosed())
- return connection;
- String connectionURL = "jdbc:mysql://" + hostname + ":" + port;
- if (database != null)
- connectionURL = connectionURL + "/" + database;
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection(connectionURL, user, password);
- return connection;
- } catch(Exception e){
- e.printStackTrace();
- }
- return null;
- }
- private void createTestTable() throws SQLException{
- if(!getConnection().getMetaData().getTables(null, null, "gracze", null).next()){
- Statement statement = getConnection().createStatement();
- statement.executeUpdate("CREATE TABLE `gracze` ("
- + "`nick` VARCHAR(36) PRIMARY KEY NOT NULL,"
- + "`haslo` VARCHAR(128) NOT NULL,"
- + "`isModerator` BIT,"
- + "`ranga` ENUM('Gracz', 'Premium', 'Szlachcic') NOT NULL)");
- statement.executeUpdate("ALTER TABLE gracze ADD INDEX (nick)");
- statement.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement