Почему я получаю сообщение об ошибке «Не найден подходящий драйвер для jdb c: h2: tcp: // localhost / ~ / userManagement»? - PullRequest
0 голосов
/ 08 февраля 2020

Я пытаюсь получить jdbc Connection, но по какой-то причине выдается сообщение об ошибке «Драйвер не найден». Используемая база данных - H2. вот код:

import com.userManagement.model.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.userManagement.model.User;



public class UserDao {

    private String jdbcUrl="jdbc:h2:tcp://localhost/~/userManagement";
    private String username="user";
    private String password="user";

    private static final String insertIntoUsers="insert into users"+"(name,address,age,email)values"+"(?,?,?,?);";
    private static final String selectUserById="select * from users where id=?";
    private static final String deleteUser="delete from users where id=?";
    private static final String updateUser="update * from users where id=?";
    private static final String selectAllUsers="select * from users";

//getting jdbc connection and returning it.
    protected Connection getConnection() {
        Connection con=null;
        try {
        con=DriverManager.getConnection(jdbcUrl, username, password);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return con;
    }

    public void insertUser(User user) {
        try(Connection con=getConnection();
                PreparedStatement pst=con.prepareStatement(insertIntoUsers)){
            pst.setString(1, user.getName());
            pst.setString(2, user.getAddress());
            pst.setInt(1, user.getAge());
            pst.setString(1, user.getEmail());
            pst.executeUpdate();
        }
        catch(Exception e) {

        }
    }
    public User selectUser(int id) {
        User user=null;
        try(Connection con=getConnection();
                PreparedStatement pst=con.prepareStatement(selectUserById)){
            pst.setInt(1,id);
            ResultSet rs=pst.executeQuery();
            while(rs.next()) {
                String name=rs.getString("name");
                String address=rs.getString("address");
                int age=rs.getInt("age");
                String email=rs.getString("email");

            user=new User(name,address,age,email);
            }

        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    public List<User> selectAllUser(){

        List <User> users=new ArrayList<> ();
        try(Connection con=getConnection();
                PreparedStatement pst=con.prepareStatement(selectAllUsers);){
            ResultSet rs=pst.executeQuery();
            while(rs.next()) {
                String name=rs.getString("name");
                String address=rs.getString("address");
                int age=rs.getInt("age");
                String email=rs.getString("email");
                users.add(new User(name,address,age,email));
            }

        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return users;
    }


    public boolean deleteUser(int id) {
        boolean rowDeleted=false;
        try(Connection con=getConnection();
                PreparedStatement pst=con.prepareStatement(deleteUser)){
            pst.setInt(1, id);
            rowDeleted=pst.executeUpdate()>0;

        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return rowDeleted;
    }

    public boolean updateUser(User user) {
    boolean rowUpdated=false;
        try(Connection con=getConnection();
                PreparedStatement pst=con.prepareStatement(updateUser)){
            pst.setString(1,user.getName());
            pst.setString(2, user.getAddress());
            pst.setInt(2, user.getAge());
            pst.setString(2, user.getEmail());
            rowUpdated=pst.executeUpdate()>0;
        }
        catch(Exception e ) {
            e.printStackTrace();
        }
        return rowUpdated;
    }

}
...