* register logic

This commit is contained in:
Lukas-Heiligenbrunner 2019-09-27 12:25:41 +02:00
parent b78f31670f
commit 06c24185d2
8 changed files with 87 additions and 23 deletions

View File

@ -7,17 +7,9 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
public class jdcb { public class jdcb {
String username; Connection conn;
String password;
String dbName;
public jdcb(String username, String password, String dbName) { public jdcb(String username, String password, String dbName) {
this.username = username;
this.password=password;
this.dbName = dbName;
}
public ResultSet executeQuery(String sql) {
Database db = new MySQLConnector( Database db = new MySQLConnector(
username, username,
password, password,
@ -25,10 +17,14 @@ public class jdcb {
3306, 3306,
dbName); dbName);
Connection c = db.getConnection(); conn = db.getConnection();
}
public ResultSet executeQuery(String sql) {
try { try {
PreparedStatement stmt = PreparedStatement stmt = conn.prepareStatement(sql);
c.prepareStatement(sql);
return stmt.executeQuery(); return stmt.executeQuery();
} catch (SQLException e) { } catch (SQLException e) {
@ -36,4 +32,15 @@ public class jdcb {
} }
return null; return null;
} }
public int executeUpdate(String sql){
try {
PreparedStatement stmt = conn.prepareStatement(sql);
return stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
} }

View File

@ -0,0 +1,20 @@
package website;
import db.jdcb;
import java.util.HashMap;
public class RegisterRequest extends PostRequest{
@Override
public String request(HashMap<String, String> params) {
System.out.println(params.toString());
String passhash = HttpTools.StringToMD5(params.get("password"));
jdcb myjd = new jdcb("users", "kOpaIJUjkgb9ur6S", "wasteinformation");
int s = myjd.executeUpdate("INSERT INTO `user` (`username`, `firstName`, `secondName`, `password`, `email`, `logindate`) VALUES ('"+params.get("username")+"', '"+params.get("firstname")+"', '"+params.get("lastname")+"', '"+passhash+"', '"+params.get("email")+"', current_timestamp());");
return "{\"accept\": true}";
}
}

View File

@ -18,6 +18,7 @@ public class Webserver {
server.createContext("/", new MainPage()); server.createContext("/", new MainPage());
server.createContext("/senddata/loginget", new LoginRequest()); server.createContext("/senddata/loginget", new LoginRequest());
server.createContext("/senddata/registerpost",new RegisterRequest());
server.setExecutor(null); // creates a default executor server.setExecutor(null); // creates a default executor
server.start(); server.start();

3
wwwroot/css/general.css Normal file
View File

@ -0,0 +1,3 @@
.hideit{
display:none;
}

View File

@ -57,7 +57,7 @@
</div> </div>
<div class="card-footer"> <div class="card-footer">
<div class="d-flex justify-content-center links"> <div class="d-flex justify-content-center links">
Don't have an account?<a id="signupbtn" href="#">Sign Up</a> Don't have an account?<a href="register.html">Sign Up</a>
</div> </div>
<div class="d-flex justify-content-center"> <div class="d-flex justify-content-center">
<a href="#">Forgot your password?</a> <a href="#">Forgot your password?</a>

View File

@ -15,13 +15,4 @@ $(document).ready(function() {
} }
},'json'); },'json');
}); });
$('#signupbtn').click(function(e) {
e.preventDefault();
$.post('/senddata/loginpost','username=luki&password=test',function(data){
console.log(data);
},'json');
});
}); });

View File

@ -1,3 +1,37 @@
$(document).ready(function() { $(document).ready(function() {
$('#loginbtn').click(function(e) {
e.preventDefault();
console.log("clicked login button");
var username = $("#usernamefield")[0].value;
var firstname = $("#firstnamefield")[0].value;
var lastname = $("#lastnamefield")[0].value;
var email = $("#emailfield")[0].value;
var password = $("#passfield")[0].value;
var replypassword = $("#replpassfield")[0].value;
if (password != replypassword) {
console.log("passwords doesnt match");
}else {
$.post('/senddata/registerpost','username='+username+
'&firstname='+firstname+
'&lastname='+lastname+
'&email='+email+
'&password='+password,function(data){
console.log(data);
if (data.accept == true) {
console.log("successfully registered!");
$("#successbar").show();
setTimeout(function() {
window.location = 'index.html';
},3000);
}else {
console.log("error!");
$("#errorbar").show();
}
},'json');
}
});
}); });

View File

@ -22,11 +22,19 @@
<!--Custom styles--> <!--Custom styles-->
<link rel="stylesheet" type="text/css" href="css/register.css"> <link rel="stylesheet" type="text/css" href="css/register.css">
<link rel="stylesheet" type="text/css" href="css/general.css">
<script type="text/javascript" src="js/register.js"></script> <script type="text/javascript" src="js/register.js"></script>
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<div id="successbar" class="alert alert-success hideit">
<strong>Success!</strong> Indicates a successful or positive action.
</div>
<div id="errorbar" class="alert alert-danger hideit">
<strong>Success!</strong> Indicates a successful or positive action.
</div>
<div class="d-flex justify-content-center h-100"> <div class="d-flex justify-content-center h-100">
<div class="card"> <div class="card">
<div class="card-header"> <div class="card-header">