To Display Database Values into Java Text Field 

Using My SQL Database(backend) and Java Swing(Front end JFrame)...

First make a Database Connection (JFrame to MYSQL ) using Following Code:

public static Connection getConnection() throws Exception{

try {

String driver="com.mysql.cj.jdbc.Driver";

String url="jdbc:mysql://127.0.0.1:3306/sch_db";

String username="root";

String password="Grace@908";

Class.forName(driver);

Connection conn=DriverManager.getConnection(url,username,password);

System.out.println("Connected");

return conn;

} catch(Exception e) {System.out.println(e);}

return null;

};

sch_db is Database schema,  download mysql database connector Driver from Net, 

 when user enters Admission No of student , and press Enter we took it as Event of Action 

performed(Right Click on Admission No field) following is the Code

AdmNo = new JTextField();

AdmNo.setBackground(Color.LIGHT_GRAY);

AdmNo.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try

{

Connection conn = getConnection();

PreparedStatement statement = conn.prepareStatement("select st_name,st_class,st_section,stream,st_gender,st_dob,stoppage from sch_db.student where substr(st_id,1,4) = "+AdmNo.getText()+" ");

ResultSet result=statement.executeQuery();

while(result.next())

{

Stu_name.setText(result.getString("st_name"));

                stu_class.setText(result.getString("st_class"));

                stu_sec.setText(result.getString("st_section"));

                stu_stream.setText(result.getString("stream"));

                stu_Gender.setText(result.getString("st_gender"));

                Stu_Dob.setText(result.getString("st_dob"));

                stoppage.setText(result.getString("stoppage"));

}}

catch(Exception ex)

{

System.out.println(e); 

     } finally {

    System.out.println(" ");

    };

    

}

}); 

if you are familiar with java code, above is simple one to understand

we are fetching database values from Student Table in sch_db database 

and display the fields in Frontend JFrame.

Try the above code to see it working....

Till then KEEP TRYING..........

Comments