Reputation: 3
Hi i am using swing for data retrieving i i am able to retrieve data on action perform.
but i want to retrieve data when i run this program automatic given id data retrieve from
database. Help me Please
Thanks
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
public class BankGui extends JApplet implements ActionListener{
// GUI components
JLabel lblUser, lblPass,lblnull,lbl1;
JTextField txtid;
JTextField txtUser;
JTextField txtPass;
JButton btnOk, btnClear;
// connections to MYSQL
private static Connection connection = null;
private static Statement statement = null;
private static ResultSet resultSet = null;
//public static Scanner in = new Scanner(System.in);
public void init(){
Container c = getContentPane();
c.setLayout( new FlowLayout() );
lblUser = new JLabel( "Username: " );
c.add( lblUser );
txtUser = new JTextField( 10 );
c.add( txtUser );
lblPass = new JLabel( "Password:" );
c.add( lblPass );
txtPass = new JTextField( 10 );
c.add( txtPass );
lbl1 = new JLabel( "Enter id" );
c.add( lbl1);
txtid=new JTextField(10);
c.add(txtid);
btnOk = new JButton( "OK" );
btnOk.addActionListener( this );
c.add( btnOk );
lblnull= new JLabel("");
c.add(lblnull);
}
@Override
public void actionPerformed(ActionEvent e) {
PreparedStatement stmt=null;
boolean isFound=false;
try{
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "");
// String sql="SELECT uname FROM users1 WHERE uname=? and pwd=?";
String sql = "SELECT * FROM users WHERE uid = '"+2+"'";
stmt=connection.prepareStatement(sql);
//stmt.getString(1,txtUser.getText());
// stmt.setString(2,txtPass.getText());
resultSet=stmt.executeQuery();
if(resultSet.next()){
isFound=true;
// String s=resultSet.getString(1);
// String s1=resultSet.getString(2);
//System.out.println(s);
txtPass.setText(resultSet.getString(1));
lblnull.setText(resultSet.getString(2));
// System.out.println(s1);
}
//
}catch(SQLException ex){
System.err.println(ex);
}finally{
if(stmt!=null){
try{
stmt.close();
}catch(Exception ex) { /* */ }
}
if(connection!=null){
try{
connection.close();
}catch(Exception ex) { /* */ }
}
}
}
static {
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception ex) {
System.err.println(ex);
}
}
}
Upvotes: 0
Views: 1281
Reputation: 17971
In addition to @AJ answer I have some tips:
Database calls are time consuming tasks and may block the Event Dispatch Thread (a.k.a. EDT) causing the GUI become unresponsive. To avoid this issue consider use a SwingWorker to perform database calls in a background thread and update Swing components in the EDT. See more in Concurrency in Swing trail.
It's not a good practice ignore exceptions in a catch block: catch(Exception ex){}
Note your query is vulnerable to SQL injection attaks. To avoid this you may want to try PreparedStatement instead.
String sql = "SELECT * FROM users WHERE uid = ?";
PreparedStatement pst = connection.prepareStatement(sql);
pst.setInt(1, 2); // see PreparedStatement.setInt(index, value)
ResultSet rs = pst.executeQuery();
Upvotes: 1
Reputation: 4534
If datatype of uid
is int
then your query should be
String sql = "SELECT * FROM users WHERE uid = "+2;
'2'
is used for varchar
datatype
Upvotes: 1