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

qacovj5a  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(472)
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)

dl5txlt9

dl5txlt91#

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

import java.sql.Connection; 
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import com.ums.beans.ems.Employee;

public class EmployeeDao {
    Connection connection;
    Statement statement;

    public EmployeeDao() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/user_1", "root", "apaar121");
    }

    public int saveEmployee(Employee employee) throws SQLException {        
        String query = "INSERT INTO user_1.employee ('name','dob','address','phone','email','education','designation','salary')VALUES (?,?,?,?,?,?,?,?);";
        PreparedStatement preparedStatement = connection.prepareStatement(query);
        preparedStatement.setString(1,employee.getName());
        preparedStatement.setString(2,employee.getAddress());
        preparedStatement.setString(3,employee.getDesignation());
        preparedStatement.setString(4,employee.getDob());
        preparedStatement.setString(5,employee.getEducation());
        preparedStatement.setString(6,employee.getEmail());
        preparedStatement.setString(7,employee.getPhone());
        preparedStatement.setFloat(8,employee.getSalary());
        return preparedStatement.executeUpdate();       
    }
}

相关问题