Java 将数据从 mysql db 检索到文本字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30162015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:14:48  来源:igfitidea点击:

Retrieve data from mysql db to textfield

javamysqlswing

提问by pktharindu

I'm trying to implement a payroll system and got some issues with it. The program needs to be able retrieve data from mysql db to jtextfield.

我正在尝试实施工资单系统,但遇到了一些问题。该程序需要能够将数据从 mysql db 检索到 jtextfield。

when I try to search I get and error saying "com.mysql.jdbc.exception.jdbc4.MySQLSyntaxErrorException. You have an error in your SQL systax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1"

当我尝试搜索时,我收到错误消息“com.mysql.jdbc.exception.jdbc4.MySQLSyntaxErrorException。您的 SQL 系统分类有错误;请检查与您的 MySQL 服务器版本相对应的手册,以获取在附近使用的正确语法” ?' 在第 1 行”

package AppPackage;

import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AllowanceGUI extends javax.swing.JFrame {

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

public AllowanceGUI() {
    initComponents();
}

private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
    conn=MySQLConnect.ConnectDb();
}                                 

private void SearchButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             


    try {
        String sql = "SELECT EmployeeID,FirstName,LastName,Designation,BasicSalary FROM EmployeeInfo WHERE EmployeeID =?";
        pst=conn.prepareStatement(sql);
        pst.setString(1,EmployeeIDSearchField.getText());

        rs = pst.executeQuery(sql);
    while(rs.next()) { 
        EmployeeIDField.setText(rs.getString("EmployeeID"));
        FirstNameField.setText(rs.getString("FirstName"));
        LasNameField.setText(rs.getString("LastName"));
        DesignationField.setText(rs.getString("Designation"));
        BasicSalaryField.setText(rs.getString("BasicSalary"));



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

}}

my db connection is as below;

我的数据库连接如下;

package AppPackage;

打包 AppPackage;

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

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

    try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://Localhost/easypay","root","");
    return conn;
    }catch(Exception e){
    JOptionPane.showMessageDialog(null, e);
    return null;

    }
}
}

can anybody please help me with this code?

有人可以帮我处理这个代码吗?

采纳答案by pktharindu

Finally found the error.

终于找到错误了。

private void SearchButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             


    try {
        String sql = "SELECT EmployeeID,FirstName,LastName,Designation,BasicSalary FROM EmployeeInfo WHERE EmployeeID =?";
        pst=conn.prepareStatement(sql);
        pst.setString(1,EmployeeIDSearchField.getText());

        rs = pst.executeQuery();
    if(rs.next()) { 
        String ID = rs.getString("EmployeeID");
        EmployeeIDField.setText(ID);
        String FN = rs.getString("FirstName");
        FirstNameField.setText(FN);
        String LN = rs.getString("LastName");
        LasNameField.setText(LN);
        String Des = rs.getString("Designation");
        DesignationField.setText(Des);
        String BS = rs.getString("BasicSalary");
        BasicSalaryField.setText(BS);

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

}

}

回答by Tschallacka

you are doing

你在做什么

   pst=conn.prepareStatement(sql);// Prepare this sql query for me
    pst.setString(1,EmployeeIDSearchField.getText());// Attach this value as first parameter
    rs = pst.executeQuery(sql);// IGNORE EVERYTHING, Execute this query

you should do pst.executeQuery();on the third line instead.

你应该pst.executeQuery();在第三行做。

EDIT

编辑

Totally read over the fact it was a select. you should just do executeQuery without the string query. P.s. for greater speed use column indexes(integers) instead of column names in your rs.next() loop.

完全阅读了它是一个选择的事实。您应该只执行没有字符串查询的 executeQuery。为了提高速度,请在 rs.next() 循环中使用列索引(整数)而不是列名。