如何使用java swing sql查询浏览记录

mnemlml8  于 2022-12-25  发布在  Java
关注(0)|答案(1)|浏览(116)

我需要在Eclipse中使用mvc架构编写的java swing项目中使用sql查询在记录之间导航。我该怎么做?请帮助我。Stackoverflow仍然要求我澄清,我不知道如何进一步解释
这是我代码

package model;

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 java.util.logging.Level;
import java.util.logging.Logger;
import controller.MainController;

public class MainModel {
    

    public Connection  connection;
    public PreparedStatement statement;
    
    private static String dburl = "jdbc:mysql://localhost:3306/stok1";
    private static String username = "root";
    private static String password = "";
    
    private String stockCode;
    private String stockName;
    private int stockType;
    private String unit;
    private String barcode;
    private double kdv;
    private String explanation;
    private String createDate;
    private ResultSet rs;
    
    public String getStockCode() {
        return stockCode;
    }
    public void setStockCode(String stockCode) {
        this.stockCode = stockCode;
    }
    public String getStockName() {
        return stockName;
    }
    public void setStockName(String stockName) {
        this.stockName = stockName;
    }
    public int getStockType() {
        return stockType;
    }
    public void setStockType(int stockType) {
        this.stockType = stockType;
    }
    public String getUnit() {
        return unit;
    }
    public void setUnit(String unit) {
        this.unit = unit;
    }
    public String getBarcode() {
        return barcode;
    }
    public void setBarcode(String barcode) {
        this.barcode = barcode;
    }
    public double getKdv() {
        return kdv;
    }
    public void setKdv(double kdv) {
        this.kdv = kdv;
    }
    public String getExplanation() {
        return explanation;
    }
    public void setExplanation(String explanation) {
        this.explanation = explanation;
    }
    public String getCreateDate() {
        return createDate;
    }
    public void setCreateDate(String createDate) {
        this.createDate = createDate;
    }
    

public class Service {
        
        private List<MainModel> models;
        
        public Service() {
            models = new ArrayList<>();
        }
        
        public void save (MainModel model) {
            models.add(model);
        }
        
        public void copy (MainModel model) {
            models.add(model);
        }
        
        public void update (MainModel model) {
            for(int i=0; i<models.size();i++) { 
            MainModel stc = models.get(i);
            if(stc.getStockCode().equals(model.getStockCode())) {
                models.set(i, model);
                break;
                }
            }
        }
        
        public void delete (MainModel model) {
            for(int i=0; i<models.size();i++) { 
                MainModel stc = models.get(i);
                if(stc.getStockCode().equals(model.getStockCode())) {
                    models.remove(i);
                    break;
                }
            }
        }
        
        public List<MainModel> getStocks(){
            
            return models;
        }

    }
    
    private Connection getConnection() {
        try {
            if(connection == null) {
                connection = DriverManager.getConnection(dburl,username,password);
            }
            
        }catch (Exception ex){
            Logger.getLogger(MainModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        return connection;
    }
    
    public void save (MainModel model) {
        
        try {
            
            statement =  getConnection().prepareStatement("select stokKodu from stockcard1 where stokKodu=?");
            statement.setString(1, model.getStockCode()); 
            ResultSet rs =statement.executeQuery();
            statement =  getConnection().prepareStatement("insert into stockcard1 values(?,?,?,?,?,?,?,?)");
            
            
            statement.setString(1, model.getStockCode());
            statement.setString(2, model.getStockName());
            statement.setInt(3, model.getStockType()+1);
            statement.setString(4, model.getUnit());
            statement.setString(5, model.getBarcode());
            statement.setDouble(6, model.getKdv());
            statement.setString(7, model.getExplanation());
            statement.setString(8,  model.getCreateDate());
            statement.executeUpdate();
            //getConnection().close();
            
        }catch (Exception ex){
            Logger.getLogger(MainModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    
        
    }
    
    public void copy (MainModel model) {
        try {
                    
                    statement =  getConnection().prepareStatement("select stokKodu from stockcard1 where stokKodu=?");
                    statement.setString(1, model.getStockCode()); 
                    ResultSet rs =statement.executeQuery();
                    statement =  getConnection().prepareStatement("insert into stockcard1 values(?,?,?,?,?,?,?,?)");
                    
                    
                    statement.setString(1, model.getStockCode());
                    statement.setString(2, model.getStockName());
                    statement.setInt(3, model.getStockType()+1);
                    statement.setString(4, model.getUnit());
                    statement.setString(5, model.getBarcode());
                    statement.setDouble(6, model.getKdv());
                    statement.setString(7, model.getExplanation());
                    statement.setString(8,  model.getCreateDate());
                    statement.executeUpdate();
                    
                }catch (Exception ex){
                    Logger.getLogger(MainModel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    
    public void update (MainModel model) {
        try {
            statement =  getConnection().prepareStatement("update stockcard1 set stokAdi=?, stokTipi=?, birimi=?, barkodu=?, kdvTipi=?, aciklama=?, olusturmaTarihi=? where stokKodu=?");
            statement.setString(1, model.getStockName());
            statement.setInt(2, model.getStockType()+1);
            statement.setString(3, model.getUnit());
            statement.setString(4, model.getBarcode());
            statement.setDouble(5, model.getKdv());
            statement.setString(6, model.getExplanation());
            statement.setString(7,  model.getCreateDate());
            statement.setString(8, model.getStockCode());
            statement.executeUpdate();
            //getConnection().close();
        }catch (Exception ex){
        }
    }
    
    public void delete (MainModel model) {
        try {
            statement =  getConnection().prepareStatement("delete from stockcard1 where stokKodu=?");
            statement.setString(1, model.getStockCode());
            statement.executeUpdate();
            //getConnection().close();
        }catch (Exception ex){
        }
    }
    
    public void nextRecord() {
        try {
            // Bir sonraki kayda geçiş yap
            if (rs.next()) {
                getStocks(barcode);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    
    
    public List<MainModel> getStocks(String Stock_Code) {
        
        try {
            
            if (Stock_Code !=null) {
                statement =  getConnection().prepareStatement("select stokKodu,stokAdi,stokTipi,birimi,barkodu,kdvTipi,aciklama,olusturmaTarihi from stockcard1 where "
                        + "stokKodu=? or stokAdi=?");
                statement.setString(1, Stock_Code);
                statement.setString(2, Stock_Code);
            }
            else {
                statement =  getConnection().prepareStatement("select * from stockcard1");
            }
            
            
            rs =statement.executeQuery();
            List<MainModel> models = new ArrayList<>();
            while(rs.next()) {
                MainModel model = new MainModel();
                model.setStockCode(rs.getString("stokKodu"));
                model.setStockCode(rs.getString("stokKodu"));
                model.setStockName(rs.getString("stokAdi"));
                model.setStockType(rs.getInt("stokTipi"));
                model.setUnit(rs.getString("birimi"));
                model.setBarcode(rs.getString("barkodu"));
                model.setKdv(rs.getDouble("kdvTipi"));
                model.setExplanation(rs.getString("aciklama"));
                model.setCreateDate(rs.getString("olusturmaTarihi"));
                models.add(model);
            }
            return models;
        }catch (Exception ex){
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return new ArrayList<>();
    }
    
}

I searched that I could navigate with the rs.next command, but I couldn't add it to my own code.

hujrc8aj

hujrc8aj1#

rs代表结果集,它是SQL查询的结果。在getStocks中,while循环遍历每一行,并将结果添加到模型列表中。您的nextRecord没有rs,因为该方法中没有变量。您调用getStocks,它以列表数组的形式返回模型列表,您可以迭代模型或只选择第一个模型单元
字符串单元=模型.get(0).getUnit
我不明白nextRecord应该做什么,但它可能还需要您首先执行一个sql查询,然后您可以执行while(rs.next()),除非您只得到一行,然后直接读取rs而不循环

相关问题