找到一条记录并将其数据检索到用户界面。 [英] Find one record and retrieve it's data to the User Interface.

查看:83
本文介绍了找到一条记录并将其数据检索到用户界面。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下语句找到mysql数据库表中存在的一条记录。过滤使用textfiled完成。我正在使用Java&我没有使用JPA。只是简单的mysql数据库连接与mysql连接器。



在下面我已经粘贴了整个java应用程序。该应用程序有2个类。



Class 01 =数据库连接



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

公共类DbConn {
public Connection conn = null;
public ResultSet rs = null;
public PreparedStatement pst = null;

public static Connection ConnDB(){
try {
Class.forName(com.mysql.jdbc.Driver);

连接conn = DriverManager.getConnection(jdbc:mysql:// localhost / naturetiles,root,);

JOptionPane.showMessageDialog(null,Connection Established);
返回conn;
} catch(异常e){
JOptionPane.showConfirmDialog(null,e);

返回null;
}
}
}







班级02 - 用户界面 - 用于插入,更新,查找,删除所需数据库的记录。





< pre lang =java> import java.sql。*;
import javax.swing.JOptionPane;

/ * *
*
* @author Roshan
* /

public class ItemDetails extends javax.swing.JInternalFrame {

public DbConn DbConnect = DbConn();
public Connection conn = DbConn.ConnDB();
ResultSet rs = null;
PreparedStatement ps = null;
public String sql;

public static void main( String [] args){
DbConn.ConnDB();
}

/ * *
*创建新表单ItemDetails
* /

public ItemDetails(){
initComponents();
}

private void Clear(){

txtICode.setText( );
txtIType.setText( );
txtISize.setText( );
txtIName.setText( );
txtIColor.setText( );
txtIStock.setText( );
txtIPrice.setText( );

}

/ * *
*这从构造函数中调用方法来初始化表单。
*警告:请勿修改此代码。此方法的内容始终由表单编辑器重新生成
*。
* /

@ SuppressWarnings 未选中
// < editor-fold defaultstate = collapseddesc =Generated Code>
private void initComponents (){

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

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt){
// TODO在此处添加您的处理代码:
}

private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt){
// TODO在此处添加您的处理代码:


sql = DELETE FROM ITEMDETAILS WHERE ItemCode = txtICode.text;
try {

ps = conn.prepareStatement(sql);
ps.execute();
JOptionPane.showMessageDialog(null, 已成功删除!);

清除();

} catch (例外e){

JOptionPane.showMessageDialog(null,e);

}

}

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

Clear();

}

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

// conn = DbConn.ConnDB() ;

sql = INSERT INTO ITEMDETAILS(
+ ItemCode,
+ ItemType,
+ ItemSize,
+ ItemName,
+ ItemColour,
+ InStock,
+ ItemPrice) + VALUES( + txtICode.getText()+ ,' + txtIType.getText()+ < span class =code-string> ',' + txtISize.getText()+ ',' + txtIName.getText()+ ',' + txtIColor.getText()+ ',' + txtIStock.getText()+ ',' + txtIPrice.getText()+ < span class =code-string> ');


try {
ps = conn.prepareStatement(sql);
ps.execute();
JOptionPane.showMessageDialog(null, 已成功保存!);

// 清除文本字段。
Clear();

// conn.close();
} < span class =code-keyword> catch (例外e){
JOptionPane.showMessageDialog(null,e);
}

}

< big>修改后的查找按钮< / big>
private void btnFindActionPerformed(java.awt.event.ActionEvent evt){
// TODO在此处添加您的处理代码:

连接dbConnection = null;
PreparedStatement preparedStatement = null;

String temp = txtSupNo.getText();

String selectSQL;
selectSQL = 从采购选择* WHERE SupNo =' + temp + ';

try {
dbConnection = DbConn.ConnDB();
preparedStatement = dbConnection.prepareStatement(selectSQL);
preparedStatement.setInt( 1 1 );

// 执行select SQL stetement
ResultSet rs = preparedStatement .executeQuery();

while (rs.next()){

String SupNo = rs.getString( SupNo);
字符串 SupName = rs.getString( SupName);

txtSupNo.setText(SupNo);
txtSupName.setText(SupName);

}

} catch (SQLException e){

System.out。的println(e.getMessage());

} 最后 {

if (preparedStatement != null){
try {
preparedStatement.close();
} catch (SQLException ex){
Logger.getLogger(Purchasing。 class .getName())。log(Level.SEVERE,null,ex);
}
}

if (dbConnection!= null){
try {
dbConnection.close();
} catch (SQLException ex){
Logger.getLogger(Purchasing。 class .getName())。log(Level.SEVERE,null,ex);
}
}

}


// 变量声明 - 不要修改
private javax.swing.JButton btnClear;
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnUpdate;
private javax.swing.JButton cmdAddData;
private javax.swing.JButton cmdFind;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel4;
private javax.swing.JTextField txtICode;
private javax.swing.JTextField txtIColor;
private javax.swing.JTextField txtIName;
private javax.swing.JTextField txtIPrice;
private javax.swing.JTextField txtISize;
private javax.swing.JTextField txtIStock;
private javax.swing.JTextField txtIType;
// 变量结束声明







我的问题是如何根据输入到文本字段的值查找特定记录,删除特定记录根据输入到文本字段的值。



谢谢

ChiranSJ



得到结果后,我想将该特定记录中的其余字段传递给我使用SWING创建的UI中的其他文本字段。



请有人帮我这样做



谢谢!



Chiransj



PS:



我已经在codeproject的伙伴的帮助下为Find按钮开发了一些代码,但是当我运行应用程序并单击find按钮并在输出窗口中我得到一个错误,即参数索引超出范围( 1>参数数量eters,这是0)。那我该怎么办?

解决方案

  String  item = txtICode。 getText(); 





要删除:



 sql =   DELETE FROM ITEMDETAILS WHERE ItemCode =' + item +  '; 





Retrive

 sql =   SELECT * FROM ITEMDETAILS WHERE ItemCode =' + item +  '; 


I want to find one record that exists on a mysql database table using the following statement. The filteration is done using a textfiled. I am using Java & i don''t use the JPA. Just simple mysql database connection with mysql connector.

At the below I have pasted the whole java application. The application has 2 classes.

Class 01 = The database connection

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

public class DbConn {
    public Connection conn= null;
    public ResultSet rs = null;
    public PreparedStatement pst =null;
   
    public static Connection ConnDB(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            
         Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/naturetiles", "root","");
         
          JOptionPane.showMessageDialog(null,"Connection Established");
         return conn;
        }catch(Exception e){
           JOptionPane.showConfirmDialog(null, e); 
           
           return null;
        }
    }
}




Class 02 - The User Interface - Used to Insert, Update, Find, Delete records of the desired database.


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

/**
 *
 * @author Roshan
 */
public class ItemDetails extends javax.swing.JInternalFrame {

    public DbConn DbConnect = new DbConn();
    public Connection conn = DbConn.ConnDB();
    ResultSet rs = null;
    PreparedStatement ps = null;
    public String sql;

    public static void main(String[] args) {
        DbConn.ConnDB();
    }

    /**
     * Creates new form ItemDetails
     */
    public ItemDetails() {
        initComponents();
    }

    private void Clear() {

        txtICode.setText("");
        txtIType.setText("");
        txtISize.setText("");
        txtIName.setText("");
        txtIColor.setText("");
        txtIStock.setText("");
        txtIPrice.setText("");

    }

    /**
     * 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() {

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

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

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        
        
        sql = "DELETE FROM ITEMDETAILS WHERE ItemCode = txtICode.text";
        try{
            
            ps = conn.prepareStatement(sql);
            ps.execute();
            JOptionPane.showMessageDialog(null, "Deleted Successfully !" );
            
            Clear();
            
        }catch(Exception e){
        
            JOptionPane.showMessageDialog(null, e);
        
        }
        
    }                                         

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

        Clear();

    }                                        

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

        //conn = DbConn.ConnDB();

        sql = "INSERT INTO ITEMDETAILS("
                + "ItemCode,"
                + "ItemType,"
                + "ItemSize,"
                + "ItemName,"
                + "ItemColour,"
                + "InStock,"
                + "ItemPrice)" + "VALUES(" + txtICode.getText() + ",'" + txtIType.getText() + "','" + txtISize.getText() + "','" + txtIName.getText() + "','" + txtIColor.getText() + "','" + txtIStock.getText() + "','" + txtIPrice.getText() + "')";


        try {
            ps = conn.prepareStatement(sql);
            ps.execute();
            JOptionPane.showMessageDialog(null, "Saved Successfully !");

            //Clear textfields.
            Clear();

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

    } 

<big>Modified Find button</big>
    private void btnFindActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        
        Connection dbConnection = null;
		PreparedStatement preparedStatement = null;
                
                String temp = txtSupNo.getText();
 
		String selectSQL;
        selectSQL = "Select * from Purchasing WHERE SupNo= '"+temp+"'";
 
		try {
			dbConnection = DbConn.ConnDB();
			preparedStatement = dbConnection.prepareStatement(selectSQL);
			preparedStatement.setInt(1, 1);
 
			// execute select SQL stetement
			ResultSet rs = preparedStatement.executeQuery();
 
			while (rs.next()) {
 
				String SupNo = rs.getString("SupNo");
				String SupName = rs.getString("SupName");
 
				txtSupNo.setText(SupNo);
                                txtSupName.setText(SupName);
 
			}
 
		} catch (SQLException e) {
 
			System.out.println(e.getMessage());
 
		} finally {
 
			if (preparedStatement != null) {
                        try {
                            preparedStatement.close();
                        } catch (SQLException ex) {
                            Logger.getLogger(Purchasing.class.getName()).log(Level.SEVERE, null, ex);
                        }
			}
 
			if (dbConnection != null) {
                        try {
                            dbConnection.close();
                        } catch (SQLException ex) {
                            Logger.getLogger(Purchasing.class.getName()).log(Level.SEVERE, null, ex);
                        }
			}
 
		}                                         


    // Variables declaration - do not modify
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JButton cmdAddData;
    private javax.swing.JButton cmdFind;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTextField txtICode;
    private javax.swing.JTextField txtIColor;
    private javax.swing.JTextField txtIName;
    private javax.swing.JTextField txtIPrice;
    private javax.swing.JTextField txtISize;
    private javax.swing.JTextField txtIStock;
    private javax.swing.JTextField txtIType;
    // End of variables declaration




My problem Is how to find an specific record that according to a value that entered to a text field, delete an specific record according to a value that entered to a text field.

Thank You
ChiranSJ

After got the result I want to pass the rest of the fields in that specific record to other textfields in my UI that created using SWING.

Please someone help me to do this

Thank You!

Chiransj

PS:

I have developed some code for the Find button with the help of a buddy in the codeproject but when I have run the application and click the find button and in the output window I got an error i.e Parameter index out of range (1 > number of parameters, which is 0)". Then what should I do?

解决方案

String item = txtICode.getText();



To Delete :

sql = "DELETE FROM ITEMDETAILS WHERE ItemCode = '"+item+"'";



To Retrive

sql = "SELECT * FROM ITEMDETAILS WHERE ItemCode = '"+item+"'";


这篇关于找到一条记录并将其数据检索到用户界面。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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