java.sql.SQLException:[Microsoft] [ODBC Microsoft Access驱动程序]参数太少.预期2 [英] java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2

查看:98
本文介绍了java.sql.SQLException:[Microsoft] [ODBC Microsoft Access驱动程序]参数太少.预期2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的javax.swing课.但是它总是抛出错误java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.不太清楚是什么原因造成的.错误在哪里?

This is my javax.swing class. But it always throws the error java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2. Not quite sure what is causing it. Where is the error?

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

public class SwingSearchApp extends JFrame implements ActionListener {

    //Initializing Components
    private static final long serialVersionUID = 1L;
    JLabel lb, lb1, lb2, lb3, lb4, lb5;
    JTextField tf1, tf2, tf3, tf4, tf5;
    JButton btn;

    //Creating Constructor for initializing JFrame components
    SwingSearchApp() {
        //Providing Title
        super("Fetching Student Information");
        lb5 = new JLabel("Enter Name:");
        lb5.setBounds(20, 20, 100, 20);
        tf5 = new JTextField(20);
        tf5.setBounds(130, 20, 200, 20);
        btn = new JButton("Submit");
        btn.setBounds(50, 50, 100, 20);
        btn.addActionListener(this);
        lb = new JLabel("Fetching Search Information From Database");
        lb.setBounds(30, 80, 450, 30);
        lb.setForeground(Color.red);
        lb.setFont(new Font("Serif", Font.BOLD, 20));
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        lb1 = new JLabel("U_Name:");
        lb1.setBounds(20, 120, 100, 20);
        tf1 = new JTextField(50);
        tf1.setBounds(130, 120, 200, 20);
        lb2 = new JLabel("U_Mail:");
        lb2.setBounds(20, 150, 100, 20);
        tf2 = new JTextField(100);
        tf2.setBounds(130, 150, 200, 20);
        lb3 = new JLabel("U_Pass:");
        lb3.setBounds(20, 180, 100, 20);
        tf3 = new JTextField(50);
        tf3.setBounds(130, 180, 200, 20);
        lb4 = new JLabel("U_Country:");
        lb4.setBounds(20, 210, 100, 20);
        tf4 = new JTextField(50);
        tf4.setBounds(130, 210, 100, 20);
        setLayout(null);
        //Add components to the JFrame
        add(lb5);
        add(tf5);
        add(btn);
        add(lb);
        add(lb1);
        add(tf1);
        add(lb2);
        add(tf2);
        add(lb3);
        add(tf3);
        add(lb4);
        add(tf4);
        //Set TextField Editable False
        tf1.setEditable(false);
        tf2.setEditable(false);
        tf3.setEditable(false);
        tf4.setEditable(false);
    }

    public void actionPerformed(ActionEvent e) {
        //Create DataBase Coonection and Fetching Records
        try {
            String str = tf5.getText();
            String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "C:\\users\\ppreeti\\employee.accdb";
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection(url, "", "");
            //Connection con = DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521", "sandeep", "welcome");
            PreparedStatement st = con.prepareStatement("select * from emp where uname=?");
            st.setString(1, str);
            //Excuting Query
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                String s = rs.getString(1);
                String s1 = rs.getString(2);
                String s2 = rs.getString(3);
                String s3 = rs.getString(4);
                //Sets Records in TextFields.
                tf1.setText(s);
                tf2.setText(s1);
                tf3.setText(s2);
                tf4.setText(s3);
            } else {
                JOptionPane.showMessageDialog(null, "Name not Found");
            }
            //Create Exception Handler
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
//Running Constructor

    public static void main(String args[]) {
        new SwingSearchApp();
    }
}

我在Access数据库中表的结构包含以下字段:- unameumailupassupcountry.

The structure of my table in Access database contains the following fields:- uname, umail, upass, upcountry.

推荐答案

如果表中不存在uname,则可能会发生这种情况.检查拼写/大小写.

This can happen if uname doesn't exist in your table. Check the spelling/case.

参考:此处

更新:

尝试稍微修改您的代码,使其能够仅运行一个简单的查询并获得结果集.然后,您可以在驱动程序看到列名称时查询它们:

Try to modify your code a little to be able to just run a simple query and get a result set. This will then allow you to query for the column names as your driver sees them:

ResultSet rs = stmt.executeQuery("SELECT * FROM emp");
ResultSetMetaData rsmd = rs.getMetaData();
String firstColumnName = rsmd.getColumnName(1);
// etc..

那肯定会告诉你.

这篇关于java.sql.SQLException:[Microsoft] [ODBC Microsoft Access驱动程序]参数太少.预期2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆