gpt4 book ai didi

java - 将java程序连接到sqlite

转载 作者:行者123 更新时间:2023-11-29 13:08:18 26 4
gpt4 key购买 nike

我有一个连接到数据库的程序,但是当我调用该表时,它说该表不存在。我已经删除了表格并重新制作了几次。我正在使用 firefox 的 sqlite 管理器插件,有一个sqlite_master 表。当我选择该表时,它不会给我任何错误...

我认为这与我创建表并向其中添加数据时添加 main 有关。

是否添加 main 作为前缀?

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

这里有人有什么想法吗?

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");

关于java - 将java程序连接到sqlite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22410117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com