当前位置: 首页 > 知识库问答 >
问题:

使用sqlite和net beans的SQL错误或缺少数据库(没有这样的表:EmployeeInfo)

澹台权
2023-03-14

这是我的系统分析和设计课程中的一个项目,所以我对Java相当陌生,只是学了一门基础课程,而数据库,从来没有学过。这是我第一次在这个网站上发帖,所以如果代码格式有点乱,我很抱歉。

下面是我的类加载DB的代码。

import java.sql.*;
import javax.swing.*;
public class javaconnect {
Connection conn=null;

public static Connection ConnecrDB()
{
    try{
        Class.forName("org.sqlite.JDBC").newInstance();
        Connection        conn=DriverManager.getConnection
        ("jdbc:sqlite:Macintosh HD\\Users\\matthewslauson\\Documents\\workspace\\scheduleSystem.sqlite" );
        JOptionPane.showMessageDialog(null, "Connection Successful");
        return conn;
    }catch(Exception e)
        {
        JOptionPane.showMessageDialog(null, e);
            return null;
        }
    }
}

到数据库的连接工作正常,但当我试图输入用户名和密码时,我得到了错误。这是我的登录屏幕的代码。我向您保证表Employeeinfo确实存在于ScheduleSystem.sqlite中。

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

    public class NewJFrame extends javax.swing.JFrame {
    Connection conn=null;
    ResultSet rs =null;
    PreparedStatement pst=null;

/**
 * Creates new form NewJFrame
 */
public NewJFrame() {
    initComponents();
    conn=javaconnect.ConnecrDB();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    txtUsername = new javax.swing.JTextField();
    txtPassword = new javax.swing.JPasswordField();
    btnLogin = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    txtUsername.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            txtUsernameActionPerformed(evt);
        }
    });

    txtPassword.setText("jPasswordField1");

    btnLogin.setText("Login");
    btnLogin.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnLoginActionPerformed(evt);
        }
    });

    jLabel1.setFont(new java.awt.Font("Sinhala Sangam MN", 1, 36)); // NOI18N
    jLabel1.setText("Welcome");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(151, 151, 151)
            .addComponent(btnLogin)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(128, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(128, 128, 128))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addComponent(jLabel1)
                    .addGap(117, 117, 117))))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(36, 36, 36)
            .addComponent(jLabel1)
            .addGap(32, 32, 32)
            .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(btnLogin)
            .addContainerGap(61, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

private void txtUsernameActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
}                                           

private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    String sql ="SELECT * FROM Employeeinfo WHERE uName=? AND pWord=?";
    try{
        pst=conn.prepareStatement(sql);
                    pst.setString(1, txtUsername.getText());
                     pst.setString(2, txtPassword.getText());

                     rs=pst.executeQuery();
                     if(rs.next()){
                         JOptionPane.showMessageDialog(null, "login credentials valid");

                     }
                     else{
                         JOptionPane.showMessageDialog(null, "login credentials invalid");
                     }
    }catch(Exception e)
        {
        JOptionPane.showMessageDialog(null, e);
            JOptionPane.showMessageDialog(null, e);
        }



}                                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton btnLogin;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPasswordField txtPassword;
private javax.swing.JTextField txtUsername;
// End of variables declaration                   
}

这是我第一次在这个网站上发帖,所以如果我的代码有点乱,我很抱歉。

共有1个答案

蒋啸
2023-03-14

我不确定在sqlite中是否类似,但在Postgresql中,必须在表名前缀模式。ex:schema_name.employeeInfo

 类似资料:
  • 大家好,我有一些问题与我的SQLite数据库在我的java程序。我试图从几个表中检索数据,但它说我的表不存在。我已经用数据库浏览器检查过了,它肯定在那里,所以我不确定我做错了什么。这是我收到的错误: [SQLITE_ERROR]SQL错误或缺少数据库(没有这样的表:staff_clocked_in.clock_in_time) 我确信我的表存在,并且两个表中都有数据,这里是我的数据库浏览器的截图。

  • 当我试图打开我的活动时,我得到了这个“未发现列”异常。错误指向上面的。 JAVAlang.RuntimeException:无法启动活动组件信息{com.text.sample/com.text.sample.usercontext.RecentsActivity}:android。数据库sqlite。SQLiteException:没有这样的列:UserName(代码1):,编译时:SELECT

  • 我有一个数据库保存在我的应用程序资产文件夹,我复制数据库使用下面的代码时,应用程序第一次打开。 上面的代码运行时没有问题,但是当您尝试查询数据库时,您会得到一个SQLite:没有这样的表异常。 这个问题只发生在Android P中,所有早期版本的Android都能正常工作。 这是一个已知的问题与Android P或有什么改变?

  • 我想使用同一种对象来查询多个表。我定义了一个基类,如下所示: 然后,我对基类进行了子类化,并为每个基类定义了DAO。 但是我一直得到一个编译错误,没有这样的表: abc,也没有这样的表: xyz。任何想法?

  • 我正在尝试使用新房间的图书馆,但我遇到了这个错误 错误:查询有问题:[SQLITE_ERROR]SQL错误或缺少数据库(没有这样的表:Station) 错误:不确定如何将游标转换为此方法的返回类型 MyDao.kt 实体

  • 我的Android应用程序中没有内置以下Dao: 当我在Android Studio3中构建项目时,我在gradle构建步骤中得到以下错误: 如果我删除loadAll函数,一切都像魅力一样工作...这个函数有什么问题?