Q.1.
Advanced Java:
A)
Write a Java Program to display the details of College(CID, CName, address,
Year)
on
JTable.
Answer :
import java.sql.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class CollegeDetailsProgram extends JFrame {
private JTable table;
public CollegeDetailsProgram() {
setTitle("College Details");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
// Create a table model with column names
String[] columnNames = {"CID", "CName", "Address", "Year"};
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
// Connect to the database and retrieve the college details
try {
// Step 1: Load the JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Step 2: Establish the connection to the database
String url = "jdbc:mysql://localhost/mydatabase";
String username = "root";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
// Step 3: Retrieve the college details from the database
String query = "SELECT * FROM college";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
while (resultSet.next()) {
String cid = resultSet.getString("CID");
String cname = resultSet.getString("CName");
String address = resultSet.getString("Address");
int year = resultSet.getInt("Year");
model.addRow(new Object[]{cid, cname, address, year});
}
// Step 4: Close the database resources
resultSet.close();
statement.close();
connection.close();
} catch (ClassNotFoundException e) {
System.out.println("JDBC driver not found.");
} catch (SQLException e) {
System.out.println("SQL exception occurred: " + e.getMessage());
}
// Create the JTable and add it to the JFrame
table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
// Show the JFrame
setVisible(true);
}
public static void main(String[] args) {
new CollegeDetailsProgram();
}
}
Output :
B)
Write a SERVLET application to accept username and password, search them into
database,
if found then display appropriate message on the browser otherwise display
error message.
Answer :
import java.io.*;
import java.sql.*;
import java.util.Properties;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection connection;
@Override
public void init() throws ServletException {
// Load database configuration from properties file
Properties props = new Properties();
try {
props.load(getServletContext().getResourceAsStream("/dbconfig.properties"));
String dbUrl = props.getProperty("db.url");
String dbUsername = props.getProperty("db.username");
String dbPassword = props.getProperty("db.password");
// Establish a connection to the database
connection = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
} catch (IOException | SQLException e) {
throw new ServletException("Failed to initialize database connection.", e);
}
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Get the username and password from the request parameters
String username = request.getParameter("username");
String password = request.getParameter("password");
// Check if the username and password are valid
boolean isValidUser = false;
try {
String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, username);
statement.setString(2, password);
ResultSet resultSet = statement.executeQuery();
isValidUser = resultSet.next();
statement.close();
} catch (SQLException e) {
throw new ServletException("Failed to search for user.", e);
}
// Send appropriate response based on search results
if (isValidUser) {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h2>Welcome, " + username + "!</h2>");
out.println("</body></html>");
} else {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid username or password.");
}
}
@Override
public void destroy() {
// Close the database connection
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException("Failed to close database connection.", e);
}
}
}
Output :
Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 23 Answers
Reviewed by technical_saurabh
on
December 15, 2022
Rating:
No comments:
Post a Comment