Also ich habe folgendes Problem, wenn ich versuche neue Einträge für die Datenbank zu machen, bleiben meine Zeilen alles auf Null(außer die ID), ich sehe ehrlich gesagt meinen Fehler nicht...Vielleicht sieht ja einer von euch den Fehler Ich bedanke mich schonmal im voraus
Hier ist der komplette Java Code für die DB:
Bild vom Table:
Log der Datenbank:
Hier ist der komplette Java Code für die DB:
Java:
package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
public class MainDatabase {
/*===== when class is used through a objekt, table is checked and created =====*/
public MainDatabase() throws Exception{
createTable();
}
/*============ creates table ===============*/
public static void createTable() throws Exception {
try {
Connection conn = getConnection();
PreparedStatement create = conn.prepareStatement("CREATE TABLE IF NOT EXISTS login (id int NOT NULL AUTO_INCREMENT, username varchar(255), email varchar(255), password varchar(255), birthday varchar(255), mtrNr varchar(255), PRIMARY KEY(id))");
create.executeUpdate();
}catch(Exception e) {
System.out.println("Error when creating Table");
e.printStackTrace();
}
finally{
System.out.println("Table successfully created");
}
}
/*============ adds entries to table ===============*/
public void addEntries(String username, String email, String password, String birthday, String mtrNr) throws Exception {
try {
Connection conn = getConnection();
PreparedStatement add = conn.prepareStatement("INSERT INTO login(username, email, password, birthday, mtrNr)VALUES(?,?,?,?,?)");
add.setString(1, username);
add.setString(2, email);
add.setString(3, password);
add.setString(4, birthday);
add.setString(5, mtrNr);
add.executeUpdate();
}catch(Exception e) {
System.out.println("Error when saving new Entry!");
e.printStackTrace();
}
}
/*============ deletes entries from table ===============*/
public void deleteEntries(String email) throws Exception {
try {
Connection conn = getConnection();
PreparedStatement delete = conn.prepareStatement("DELETE FROM login WHERE email = ?");
delete.setString(1, email);
delete.executeUpdate();
}catch(Exception e) {
System.out.println("Error when deleting Entrie");
e.printStackTrace();
}
}
/*============ fetches entries from table ===============*/
public ArrayList<String> getEntries(String username, String password) throws Exception{
try {
Connection conn = getConnection();
PreparedStatement getEntries = conn.prepareStatement("SELECT username, password FROM login");
ResultSet result = getEntries.executeQuery();
ArrayList<String> returnArray = new ArrayList<String>();
while(result.next()) {
System.out.println(result.getString("username") + " " + result.getString("password"));
returnArray.add(result.getString("username"));
}
System.out.println("All records have been selected!");
return returnArray;
}catch(Exception e) {
System.out.println("Error when loading Entries");
e.printStackTrace();
}
return null;
}
/*============ sets up connection to SQL database ===============*/
public static Connection getConnection() throws Exception{
try {
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/UniDB?useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC";
String username = "testuser";
String password = "Test123456789";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
System.out.println("Connected");
return conn;
}catch(Exception e) {
System.out.println("Connection Failed");
e.printStackTrace();
}
return null;
}
}
Bild vom Table:
Log der Datenbank: