Help to connect Java with an Access DB

0

I made a Java connection with Access, and the connection is already done correctly, then I made an interface through which I can insert, edit and save data for the database but it does not work for me, I'll leave the connection code:

package Conexion;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JOptionPane;

public class ConexionAccess {
    static Connection con = null;
    static String driver = "net.ucanaccess.jdbc.UcanaccessDriver";
    static String url ="jdbc:ucanaccess://C:\Users\Administrador\Documents\Mueblelo.accdb;memory=false";

    public static Connection getConexion (){    
        try {
            if (con==null) {    
                Class.forName(driver);
                con = DriverManager.getConnection (url);
                JOptionPane.showMessageDialog(null,"Conexion correcta");
            }
        }catch (Exception ex) {
            ex.printStackTrace ();
            con = null;                 
            } 
            return con;     
        }

        public static void main (String []args){

            Connection con = ConexionAccess.getConexion();          
        }

    }

And this is the interface code:

package Conexion;
import java.awt.Color;
import java.awt.GraphicsConfiguration;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import java.awt.Image;
import javax.swing.ImageIcon;

public class FrmPrueba extends JFrame {


      ;

     {
        // TODO Auto-generated method stub

    }
    JTable tblPrueba;
    JScrollPane jspPrueba;
    DefaultTableModel dtmPrueba;
    String titulos [] = {"ID", "Precio", "Categoria", "Descripcion", "Precio A"};

    JTextField txtID, txtPrecio, txtCategoria, txtDescripcion, txtPrecioA;
    JLabel lblID, lblPrecio, lblCategoria, lblDescripcion, lblPrecioA;

    Connection con = null;
    JButton btnGuardar, btnNuevo;   

    public FrmPrueba (){

        //setVisible(true);

        btnGuardar = new JButton("Guardar");
        btnNuevo = new JButton("Nuevo");

        txtID = new JTextField();
        txtPrecio = new JTextField();
        txtCategoria = new JTextField();
        txtDescripcion = new JTextField();
        txtPrecioA = new JTextField();


        lblID = new JLabel("ID");
        lblPrecio = new JLabel("Precio");
        lblCategoria = new JLabel("Categoria");
        lblDescripcion = new JLabel("Descripcion");
        lblPrecioA = new JLabel("Precio A");


        dtmPrueba = new DefaultTableModel(null, titulos);
        tblPrueba = new JTable(dtmPrueba);
        jspPrueba = new JScrollPane (tblPrueba);    

        add(jspPrueba);

        add(txtID);
        add(txtPrecio);
        add(txtCategoria);
        add(txtDescripcion);
        add(txtPrecioA);
        add(lblID);
        add(lblPrecio);
        add(lblCategoria);
        add(lblDescripcion);
        add(lblPrecioA);

        add(btnGuardar);
        add(btnNuevo);

        jspPrueba.setBounds(20,200,550,200);

        lblID.setBounds(20,20,200,20);
        txtID.setBounds(120,20,200,20);

        lblPrecio.setBounds(20,50,100,20);
        txtPrecio.setBounds(120,50,200,20);

        lblCategoria.setBounds(20,80,100,20);
        txtCategoria.setBounds(120,80,200,20);

        lblDescripcion.setBounds(20,110,100,20);
        txtDescripcion.setBounds(120,110,200,20);

        lblPrecioA.setBounds(20,140,110,20);
        txtPrecioA.setBounds(120,140,200,20);

        btnNuevo.setBounds(390, 100, 100, 25);
        btnGuardar.setBounds(390, 140, 100, 25);

        btnGuardar.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                btnGuardarActionPerform(evt);
            }
        });

        btnNuevo.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                btnNuevoActionPerformed(evt);
            }
        });


        tblPrueba.addMouseListener(new MouseAdapter(){
            public void MouseClicked(MouseEvent evt){
                tblPruebaMouseClicked(evt);
            }
        });
        mostrar();

        }

        protected void btnNuevoActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
            txtID.setText("");
            txtPrecio.setText("");
            txtCategoria.setText("");
            txtDescripcion.setText("");
            txtPrecioA.setText("");
    }

        protected void tblPruebaMouseClicked(MouseEvent evt) {
        // TODO Auto-generated method stub
            int file = tblPrueba.rowAtPoint(evt.getPoint());
            txtID.setText(tblPrueba.getValueAt(file, 0).toString());
            txtPrecio.setText(tblPrueba.getValueAt(file, 1).toString());
            txtCategoria.setText(tblPrueba.getValueAt(file, 2).toString());
            txtDescripcion.setText(tblPrueba.getValueAt(file, 3).toString());
            txtPrecioA.setText(tblPrueba.getValueAt(file, 4).toString());

    }

    protected void btnGuardaractionPerformed(ActionEvent evt) {
        try{
            con = ConexionAccess.getConexion();
            if(txtID.getText().equals("")){
                String sql = "Insert into tbl(Id, Precio, Categoria, Descripcion, PrecioA values (?, ?, ?, ?)";
                PreparedStatement pst = con.prepareStatement(sql);
                pst.setString(1, txtID.getText());
                pst.setString(2, txtPrecio.getText());
                pst.setString(3, txtCategoria.getText());
                pst.setString(4, txtDescripcion.getText());
                pst.setString(5, txtPrecioA.getText());
                int n = pst.executeUpdate();
                if(n>0){
                    JOptionPane.showMessageDialog(rootPane, "Se registró de forma correcta");
                }

            }else{
                String sql = "update tbl set Id = ?, set = Precio, Categoria = ?, Descripcion = ?, PrecioA = ? where Id = ?)";  
                PreparedStatement pst = con.prepareStatement(sql);
                pst.setString(1, txtID.getText());
                pst.setString(2, txtPrecio.getText());
                pst.setString(3, txtCategoria.getText());
                pst.setString(4, txtDescripcion.getText());
                pst.setInt(5, Integer.parseInt(txtPrecioA.getText()));
                int n = pst.executeUpdate();
                if(n>0){
                    JOptionPane.showMessageDialog(rootPane, "Se actualizó de forma correcta");
                }
            }}catch(Exception ex){
                ex.printStackTrace();
        }

        mostrar();
        txtID.setText("");
        txtPrecio.setText("");
        txtCategoria.setText("");
        txtDescripcion.setText("");
        txtPrecioA.setText("");
        mostrar();

    }   

    public void mostrar (){
        try{
            con = ConexionAccess.getConexion();
            DefaultTableModel MUEBLELOS= (DefaultTableModel) tblPrueba.getModel();
            String dts[] = new String[5];
            String sql = "INSERT INTO table_name VALUES(value1, value2, value3)";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()); 
                dts[0] = rs.getString(1);
                dts[1] = rs.getString(2);
                dts[2] = rs.getString(3);
                dts[3] = rs.getString(4);
                dts[4] = rs.getString(5);
                MUEBLELOS.addRow(dts); 

          tblPrueba.setModel(MUEBLELOS);     
        }catch(Exception ex){
            ex.printStackTrace();
    }
    }
    public static void main(String [] args){
        FrmPrueba mi = new FrmPrueba(); 
        mi.getContentPane().setBackground(Color.WHITE);
        mi.setDefaultCloseOperation(EXIT_ON_CLOSE);
        mi.setLayout (null);
        mi.setTitle ("Mueblelo | ¡Bienvenido!");
        mi.setSize(600, 500);
        mi.setVisible(true);
        //Connection on = ConexionAccess.obtenerConexion();


            }   

   public FrmPrueba(String title, GraphicsConfiguration gc, JTable tblPrueba,
            JScrollPane jspPrueba, DefaultTableModel dtmPrueba,
            String[] titulos, JTextField txtId, JTextField txtPrecio,
            JTextField txtCategoria, JTextField txtDescripcion,
            JTextField txtPrecioA, JLabel lblId, JLabel lblPrecio,
            JLabel lblCategoria, JLabel lblDescripcion, JLabel lblPrecioA,
            Connection con, JButton btnGuardar, JButton btnNuevo) {
        super(title, gc);
        this.tblPrueba = tblPrueba;
        this.jspPrueba = jspPrueba;
        this.dtmPrueba = dtmPrueba;
        this.titulos = titulos;
        this.txtID = txtId;
        this.txtPrecio = txtPrecio;
        this.txtCategoria = txtCategoria;
        this.txtDescripcion = txtDescripcion;
        this.txtPrecioA = txtPrecioA;
        this.lblID = lblId;
        this.lblPrecio = lblPrecio;
        this.lblCategoria = lblCategoria;
        this.lblDescripcion = lblDescripcion;
        this.lblPrecioA = lblPrecioA;
        this.con = con;
        this.btnGuardar = btnGuardar;
        this.btnNuevo = btnNuevo;
    }

protected void btnGuardarActionPerforme (ActionEvent evt) {


    }




    protected void btnGuardarActionPerform(ActionEvent evt) {


    }


    }

Running the connection with Access is no problem, but when running the interface, it appears but I miss the following errors besides that the data is not saved when entering:

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.4 usuario no tiene privilegios suficientes o objeto no encontrado: TABLE_NAME
    at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:218)
    at Conexion.FrmPrueba.mostrar(FrmPrueba.java:196)
    at Conexion.FrmPrueba.<init>(FrmPrueba.java:123)
    at Conexion.FrmPrueba.main(FrmPrueba.java:211)
Caused by: java.sql.SQLSyntaxErrorException: usuario no tiene privilegios suficientes o objeto no encontrado: TABLE_NAME
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.executeQuery(Unknown Source)
    at net.ucanaccess.jdbc.UcanaccessStatement.executeQuery(UcanaccessStatement.java:216)
    ... 3 more
Caused by: org.hsqldb.HsqlException: usuario no tiene privilegios suficientes o objeto no encontrado: TABLE_NAME
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.SchemaManager.getTable(Unknown Source)
    at org.hsqldb.ParserDQL.readTableName(Unknown Source)
    at org.hsqldb.ParserDQL.readRangeVariableForDataChange(Unknown Source)
    at org.hsqldb.ParserDML.compileInsertStatement(Unknown Source)
    at org.hsqldb.ParserCommand.compilePart(Unknown Source)
    at org.hsqldb.ParserCommand.compileStatements(Unknown Source)
    at org.hsqldb.Session.executeDirectStatement(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 6 more

I'm using Eclipse, I'd really appreciate it if you helped me out!

    
asked by LRHDZ 22.08.2018 в 19:04
source

0 answers