简体   繁体   中英

Connecting java program to sqlite

I have a program connecting to DB but when I call the table it says the tables does not exist. I have deleted table and remade it a few times. I am using the sqlite manager plugin for firefox, there is a sqlite_master table. When I select that table it does not give me any errors...

I think it has to do with it adding main when I create the table and add data to it.

is it adding main as a prefix?

INSERT INTO "main"."contacts" ("Business_Name","First_Name","Last_Name","Phone","Email","Address_Line_1","Address_Line_2","Website") VALUES (?1,?2,?3,?4,?5,?6,?7,?8)
Parameters:
param 1 (text): Texcom
param 2 (text): Jesse
param 3 (text): Lovelace
param 4 (text): 817-555-9999
param 5 (text): jlove@gmail.com
param 6 (text): 852 hemming way
param 7 (text): Colleyville, TX 76034
param 8 (text): www.texcom.com

Anyone have any ideas here?

import javax.swing.*;
import java.sql.*;

public class DbConnect {

    Connection conn = null;
    public static Connection ConnectDb() {

        try{
            Class.forName("org.sqlite.JDBC");
            Connection conn = DriverManager.getConnection("jdbc:sqlite:systemDb.Documents\\workspace\\DiazProject4\\addressBook.sqlite");
            JOptionPane.showMessageDialog(null, "DB Connected");        
                    return conn;
        }catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
        }
    }


import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.*;

import java.sql.*;

import net.proteanit.sql.DbUtils;

public class Driver {

    private JFrame f;

    private JPanel p;

    private JTextField fieldBN;
    private JTextField fieldFN;
    private JTextField fieldLN;
    private JTextField fieldP;
    private JTextField fieldE;
    private JTextField fieldA;
    private JTextField aLine2;
    private JTextField fieldW;

    private JLabel labelBN;
    private JLabel labelFN;
    private JLabel labelLN;
    private JLabel labelP;
    private JLabel labelE;
    private JLabel labelA;
    private JLabel labelW;

    private JButton buttonS;

    private JTable tableDisplay;

    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

    // Constructor:

    public Driver() {       
        gui();      
        conn = DbConnect.ConnectDb();
        UpdateTable();
    }

    public void gui() { 
        f = new JFrame("Contact Book");


        GridBagConstraints c = new GridBagConstraints();  

        c.insets = new Insets(10, 10, 10, 10);

        p = new JPanel(new GridBagLayout());
        f.getContentPane().add(p, BorderLayout.NORTH);

    c.gridx = 0;
    c.gridy = 0;
    labelBN = new JLabel ("Business Name:");
    p.add(labelBN, c);  

    c.gridx = 10;
    c.gridy = 0;
    fieldBN = new JTextField(10);
    p.add(fieldBN, c);

    c.gridx = 0;
    c.gridy = 10;
    labelFN= new JLabel ("First Name:");
    p.add(labelFN, c);      

    c.gridx = 10;
    c.gridy = 10;
    fieldFN = new JTextField (10);
    p.add(fieldFN, c);

    c.gridx = 0;
    c.gridy = 20;
    labelLN= new JLabel ("Last Name:");
    p.add(labelLN, c);      

    c.gridx = 10;
    c.gridy = 20;
    fieldLN = new JTextField (10);
    p.add(fieldLN, c);

    c.gridx = 0;
    c.gridy = 30;
    labelP = new JLabel ("Phone Number:");
    p.add(labelP, c);

    c.gridx = 10;
    c.gridy = 30;
    fieldP = new JTextField (10);
    p.add(fieldP, c);

    c.gridx = 0;
    c.gridy = 40;
    labelE = new JLabel ("Email:");
    p.add(labelE, c);

    c.gridx = 10;
    c.gridy = 40;
    fieldE = new JTextField (10);
    p.add(fieldE, c);

    c.gridx = 0;
    c.gridy = 50;                           
    labelA = new JLabel ("Address:");
    p.add(labelA, c);

    c.gridx = 10;
    c.gridy = 50;
    fieldA = new JTextField (10);
    p.add(fieldA, c);
    c.gridx = 10;
    c.gridy = 60;
    aLine2 = new JTextField (10);
    p.add(aLine2, c);

    c.gridx = 0;
    c.gridy = 70;
    labelW = new JLabel ("Website:");
    p.add(labelW, c);

    c.gridx = 10;
    c.gridy = 70;
    fieldW = new JTextField (10);
    p.add(fieldW, c);

    c.gridx = 10;
    c.gridy = 80;
    buttonS = new JButton("Save:");
    p.add(buttonS, c);

    c.gridx = 10;
    c.gridy = 90;
    tableDisplay = new JTable(8, 2);
    p.add(tableDisplay, c);

        // pack the frame for better cross platform support
        f.pack();
        // Make it visible
        f.setVisible(true);
        f.setSize(500,800); // default size is 0,0
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } // End of Gui Method

    private void UpdateTable() {
        try {
        String sql = "SELECT * FROM entries";
        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery();
        tableDisplay.setModel(DbUtils.resultSetToTableModel(rs));
    }

    catch(Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
           public void run() {
                new Driver();

           }
        });
    } // End main Method

       } // End class Driver

请检查您的连接字符串,检查完整路径:

DriverManager.getConnection("jdbc:sqlite:YOUR_DATABASE_PATH");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM