为什么我会得到错误消息java.lang.runtimeexception:java.sql.sqlexception:no database selected in my java code(linking with mysql database)

qacovj5a  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(534)
  1. package persistentie;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import domein.Speler;
  10. public class SpelerMapper {
  11. private List<Speler> spelers = new ArrayList<>();
  12. private List<Integer> scoreSpeler = new ArrayList<>();
  13. private static final String UPDATE_SCORE = "insert into ID343589_g52.scoreSpeler(spelerNr,score) values(?,?)";
  14. public SpelerMapper() {
  15. try (Connection conn = DriverManager.getConnection(Connectie.JDBC_URL);
  16. PreparedStatement query = conn.prepareStatement("SELECT * FROM ID343589_g52.speler");
  17. ResultSet rs = query.executeQuery()) {
  18. while (rs.next()) {
  19. String gebruikersnaam = rs.getString("gebruikersNaam");
  20. String wachtwoord = rs.getString("wachtwoord");
  21. spelers.add(new Speler(gebruikersnaam, wachtwoord));
  22. }
  23. } catch (SQLException ex) {
  24. throw new RuntimeException(ex);
  25. }
  26. }
  27. public boolean correcteGegevens(Speler nieuweSpeler) {
  28. return spelers.contains(nieuweSpeler);
  29. }
  30. public List<Integer> geefscoreSpeler(String naam) {
  31. scoreSpeler.clear();
  32. try (Connection conn = DriverManager.getConnection(Connectie.JDBC_URL);
  33. 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));
  34. ResultSet rs = query.executeQuery()) {
  35. while (rs.next()) {
  36. int score = rs.getInt("score");
  37. scoreSpeler.add(score);
  38. }
  39. } catch (SQLException ex) {
  40. throw new RuntimeException(ex);
  41. }
  42. return scoreSpeler;
  43. }
  44. public void slaScoreOp(int score, String naam) {
  45. try (Connection conn = DriverManager.getConnection(Connectie.JDBC_URL);
  46. 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));
  47. ResultSet rs = query.executeQuery()) {
  48. int id = 0;
  49. while (rs.next()) {
  50. id = rs.getInt("score");
  51. }
  52. PreparedStatement update = conn.prepareStatement(UPDATE_SCORE);
  53. update.setInt(1, id);
  54. update.setInt(2, score);
  55. update.executeUpdate();
  56. } catch (SQLException ex) {
  57. throw new RuntimeException(ex);
  58. }
  59. }
  60. }

我在这里做的任何蠢事真的不是怎么回事吗?我真的是一个新的连接数据库与我的java项目?在methode geefscorespeler()中,我要做的事情是获得一个i个人玩家的分数。在methode slascoreop()中,我尝试在一个表中插入一个新列(还尝试通过一个innerjoins表获取用户名的id)

dl5txlt9

dl5txlt91#

嘿,我做了一个用户管理系统,这是我的dao(数据库连接类),我在这里使用了prepared语句,看看它是否有用。

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import com.ums.beans.ems.Employee;
  7. public class EmployeeDao {
  8. Connection connection;
  9. Statement statement;
  10. public EmployeeDao() throws ClassNotFoundException, SQLException {
  11. Class.forName("com.mysql.cj.jdbc.Driver");
  12. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/user_1", "root", "apaar121");
  13. }
  14. public int saveEmployee(Employee employee) throws SQLException {
  15. String query = "INSERT INTO user_1.employee ('name','dob','address','phone','email','education','designation','salary')VALUES (?,?,?,?,?,?,?,?);";
  16. PreparedStatement preparedStatement = connection.prepareStatement(query);
  17. preparedStatement.setString(1,employee.getName());
  18. preparedStatement.setString(2,employee.getAddress());
  19. preparedStatement.setString(3,employee.getDesignation());
  20. preparedStatement.setString(4,employee.getDob());
  21. preparedStatement.setString(5,employee.getEducation());
  22. preparedStatement.setString(6,employee.getEmail());
  23. preparedStatement.setString(7,employee.getPhone());
  24. preparedStatement.setFloat(8,employee.getSalary());
  25. return preparedStatement.executeUpdate();
  26. }
  27. }
展开查看全部

相关问题