02
Апр
2021

Поиск java по Базе данных

Всем привет. Создал базу с помощью библиотеки sqlite jdbc. Всё в одном классе.

import java.sql.*;

public class Main {
    public static void main(String[] args) {
        Connection connection = null;
        Statement stmtFirst = null;
        Statement stmtSecond = null;

        try
        {
            connection = DriverManager.getConnection("jdbc:sqlite:text.db");
            System.out.println("База данных успешно открыта");
            stmtFirst = connection.createStatement();
            String sql2 = "UPDATE completed_work set LASTNAME = 'Петров' where ID = 1;";
            stmtFirst.executeUpdate(sql2);

            stmtFirst = connection.createStatement();
            String sql3 = "DELETE from completed_work where ID = 23";
            stmtFirst.executeUpdate(sql3);


            stmtFirst = connection.createStatement();
            ResultSet resultSet = stmtFirst.executeQuery("SELECT * FROM completed_work;");

            while(resultSet.next()) {
                int id = resultSet.getInt("ID");
                String FIRSTNAME = resultSet.getString("FIRSTNAME");
                String LastName = resultSet.getString("LASTNAME");
                String desc = resultSet.getString("desc");

                System.out.println("Идентификатор = " + id);
                System.out.println("Имя = " + FIRSTNAME);
                System.out.println("Фамилия = " + LastName);
                System.out.println("Описание = " + desc);
            }
            String sql;
            sql = "CREATE TABLE IF NOT EXISTS completed_work" +

                    "(ID INTEGER PRIMARY KEY AUTOINCREMENT ," +
                    "FIRSTNAME TEXT NOT NULL," +
                    "LASTNAME TEXT NOT NULL," +
                    "desc TEXT NOT NULL)";
          sql = "INSERT INTO completed_work ( FIRSTNAME, LASTNAME, desc)" + "VALUES ('Иван','Иванов','Замена масла в КПП');";
            stmtFirst.executeUpdate(sql);
            sql = "INSERT INTO completed_work (FIRSTNAME, LASTNAME, desc)" + "VALUES ('Пётр','Ильин','Замена рулевой рейки');";
            stmtFirst.executeUpdate(sql);
            sql = "INSERT INTO completed_work ( FIRSTNAME, LASTNAME, desc)" + "VALUES ('Игорь','Пирогов','Замена дроссельной заслонки');";
            stmtFirst.executeUpdate(sql);
            sql = "INSERT INTO completed_work (  FIRSTNAME, LASTNAME, desc)" + "VALUES ('Иван','Воробьёв','Замена датчика холостого хода');";
            stmtFirst.executeUpdate(sql);
            stmtFirst.execute(sql);
            stmtFirst.close();



            stmtSecond = connection.createStatement();
            String sql1;
            sql1 = "CREATE TABLE IF NOT EXISTS employee" +
                    "(\n" +
                    "    " +
                    "EMPLOYEEID INTEGER PRIMARY KEY AUTOINCREMENT\n" +
                    ")\n" +
                    "\n";
           stmtSecond = connection.createStatement();
            sql1 = "INSERT INTO employee (EMPLOYEEID)" + "VALUES (?);";
            stmtSecond.executeUpdate(sql1);
            sql1 = "INSERT INTO employee (EMPLOYEEID)" + "VALUES (?);";
            stmtSecond.executeUpdate(sql1);
            sql1 = "INSERT INTO employee (EMPLOYEEID)" + "VALUES (?);";
            stmtSecond.executeUpdate(sql1);
            sql1 = "INSERT INTO employee (EMPLOYEEID)" + "VALUES (?);";
            stmtSecond.executeUpdate(sql1);


            stmtSecond.execute(sql1);
            stmtFirst.execute(sql);
            stmtSecond.close();
            connection.close();
        }
        catch (Exception e)
        {
            System.err.println(e.getClass().getName() + ": " +e.getMessage());
            System.exit(0);
        }
        System.out.println("Таблица успешно создана");
    }
}

Как можно сделать поиск? я делаю наподобие удаления либо обновления, SELECT * FROM completed_work WHERE ID=2 - вот мой запрос, но он ничего не выводит.

Источник: https://ru.stackoverflow.com/questions/1265075/%D0%9F%D0%BE%D0%B8%D1%81%D0%BA-java-%D0%BF%D0%BE-%D0%91%D0%B0%D0%B7%D0%B5-%D0%B4%D0%B0%D0%BD%D0%BD%D1%8B%D1%85

Тебе может это понравится...

Добавить комментарий