OneCompiler

JAVA 10

114

10 Current Date. && record from student table

Q1]

DateService.java

package com.example.Dateservices;
import java.util.Date;
public class DateService
{

     public String getCurrentDate()
 {
        Date currentDate = new Date();
        return "Current Date: " + currentDate.toString();
    }

}

App.java

package com.example.services.DateService;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

 
    com.example.services.DateService.DateService dateService = (com.example.services.DateService.DateService) context.getBean("DateService");

  
    System.out.println(dateService.getCurrentDate());

  
    ((ClassPathXmlApplicationContext) context).close();
    
    
}

}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="DateService" class="com.example.services.DateService.DateService"/>
</beans>

Q2]

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

public class StudentRecordDisplay extends JFrame {
private JTextField rnoField, snameField, perField;
private JButton displayButton;

public StudentRecordDisplay() {
    setTitle("Student Record Display");
    setSize(300, 150);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel(new GridLayout(4, 2));

    JLabel rnoLabel = new JLabel("Roll Number:");
    rnoField = new JTextField();
    rnoField.setEditable(false);
    panel.add(rnoLabel);
    panel.add(rnoField);

    JLabel snameLabel = new JLabel("Student Name:");
    snameField = new JTextField();
    snameField.setEditable(false);
    panel.add(snameLabel);
    panel.add(snameField);

    JLabel perLabel = new JLabel("Percentage:");
    perField = new JTextField();
    perField.setEditable(false);
    panel.add(perLabel);
    panel.add(perField);

    displayButton = new JButton("Display Record");
    displayButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            displayRecord();
        }
    });
    panel.add(displayButton);

    add(panel);
    setVisible(true);
}

private void displayRecord() {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(("jdbc:postgresql:database-name:postgres:password));

        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT * FROM student LIMIT 1");

        if (rs.next()) {
            rnoField.setText(rs.getString("RNo"));
            snameField.setText(rs.getString("SName"));
            perField.setText(rs.getString("Per"));
        } else {
            JOptionPane.showMessageDialog(this, "No records found in the student table.");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
            if (conn != null) conn.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        new StudentRecordDisplay();
    });
}

}