package persistentie;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import domein.Speler;
public class SpelerMapper {
private List<Speler> spelers = new ArrayList<>();
private List<Integer> scoreSpeler = new ArrayList<>();
private static final String UPDATE_SCORE = "insert into ID343589_g52.scoreSpeler(spelerNr,score) values(?,?)";
public SpelerMapper() {
try (Connection conn = DriverManager.getConnection(Connectie.JDBC_URL);
PreparedStatement query = conn.prepareStatement("SELECT * FROM ID343589_g52.speler");
ResultSet rs = query.executeQuery()) {
while (rs.next()) {
String gebruikersnaam = rs.getString("gebruikersNaam");
String wachtwoord = rs.getString("wachtwoord");
spelers.add(new Speler(gebruikersnaam, wachtwoord));
}
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
public boolean correcteGegevens(Speler nieuweSpeler) {
return spelers.contains(nieuweSpeler);
}
public List<Integer> geefscoreSpeler(String naam) {
scoreSpeler.clear();
try (Connection conn = DriverManager.getConnection(Connectie.JDBC_URL);
PreparedStatement query = conn.prepareStatement(String.format("SELECT sc.score FROM ID343589_g52.scoreSpeler sc join speler s on s.spelernr = sc.spelerNr where s.gebruikersNaam = '%s'", naam));
ResultSet rs = query.executeQuery()) {
while (rs.next()) {
int score = rs.getInt("score");
scoreSpeler.add(score);
}
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
return scoreSpeler;
}
public void slaScoreOp(int score, String naam) {
try (Connection conn = DriverManager.getConnection(Connectie.JDBC_URL);
PreparedStatement query = conn.prepareStatement(String.format("SELECT sc.score FROM ID343589_g52.scoreSpeler sc join speler s on s.spelernr = sc.spelerNr where s.gebruikersNaam = '%s'", naam));
ResultSet rs = query.executeQuery()) {
int id = 0;
while (rs.next()) {
id = rs.getInt("score");
}
PreparedStatement update = conn.prepareStatement(UPDATE_SCORE);
update.setInt(1, id);
update.setInt(2, score);
update.executeUpdate();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
}
我在这里做的任何蠢事真的不是怎么回事吗?我真的是一个新的连接数据库与我的java项目?在methode geefscorespeler()中,我要做的事情是获得一个i个人玩家的分数。在methode slascoreop()中,我尝试在一个表中插入一个新列(还尝试通过一个innerjoins表获取用户名的id)
1条答案
按热度按时间dl5txlt91#
嘿,我做了一个用户管理系统,这是我的dao(数据库连接类),我在这里使用了prepared语句,看看它是否有用。