Я пытаюсь вставить данные с использованием DAO & GUI в базу данных (mysql), запрос выполнен, но он не обновляется в базе данных - PullRequest
0 голосов
/ 11 октября 2019
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package daoexample;

import java.sql.*;

/**
 *
 * @author pc
 */
public class DAOExample {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }

}

class TransactionDAO {

    Connection con = null;

    public Connection getConnection() {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/transaction", "root", "");

        } catch (Exception e) {
            System.out.println(e);
        }
        return con;

    }

    public Transaction insert(Transaction t) {

        try {
            String q = "insert into trt values(?,?,?,?)";
            Class.forName("com.mysql.jdbc.Driver");
            Connection con
                    = DriverManager.getConnection("jdbc:mysql://localhost:3306/transaction", "root", "");

            PreparedStatement pst = con.prepareStatement(q);
            pst.setInt(1, t.id);
            pst.setInt(2, t.Ac_no);
            pst.setInt(3, t.serve_by);
            pst.setInt(4, t.amount);

            int rs = pst.executeUpdate();
            if (rs > 0) {
                System.out.println("Inserted Sucessfully");
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return t;
    }

    public Transaction update(Transaction t) {

        try {
            String q = "update trt set Ac_no=?,serve_by=?,amount=? where id=?";
            Class.forName("com.mysql.jdbc.Driver");
            Connection con
                    = DriverManager.getConnection("jdbc:mysql://localhost:3306/transaction", "root", "");

            PreparedStatement pst = con.prepareStatement(q);
            pst.setInt(1, t.id);
            pst.setInt(2, t.Ac_no);
            pst.setInt(3, t.serve_by);
            pst.setInt(4, t.amount);

            int rs = pst.executeUpdate();
            if (rs > 0) {
                System.out.println("Inserted Sucessfully");
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return t;
    }

}

class Transaction {

    int id;
    int amount;
    int Ac_no;
    int serve_by;

    public void setid(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setamount(int amount) {
        this.amount = amount;
    }

    public int getAmount() {
        return amount;
    }

    public void setAc(int Ac_no) {
        this.Ac_no = Ac_no;
    }

    public int getAc() {
        return Ac_no;
    }

    public void setServe(int serve_by) {
        this.serve_by = serve_by;
    }

    public int getServe() {
        return serve_by;
    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...