Worksheet-3.2 Java PDF New
Worksheet-3.2 Java PDF New
Experiment- 3.2
1. Aim : Create an application for online auction using Servlet and JSP.
3. Procedure :
1. Begin by initiating a Dynamic Web Project in Eclipse IDE.
2. Develop a JSP le within the project.
3. Launch the Tomcat server and deploy the project.
4. Initiate a Dynamic Web Project.
5. Develop a JSP le.
6. 6. Launch the Tomcat server and deploy the project..
Program Code :
package com.company;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
fi
fi
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.*;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
public
class Admin {
private JButton startButton;
private JLabel timerLabel;
privateJPanel adminPanel;
private JButton ADDITEMButton;
Private JTable table1;
private JTextField nameData;
private JTextField priceData;
private JTextField path;
private JButton SELECTIMAGEButton;
private JLabel imageLabel;
private JButton CLOSEButton;
public static String adminNameData = "";
public static String adminPriceData = "";
public static ImageIcon adminImageData;
JFrame adminF = new JFrame();
Timer timer;
public static int sec = 60;
public Admin() {
adminF.setContentPane(adminPanel);
adminF.pack();
tableData();
adminF.setVisible(true);
startButton.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
startTimer();
timer.start();
}
});
ADDITEMButton.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
if (nameData.getText().equals("") || path.getText().equals("") ||
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
priceData.getText().equals("")) {
JOptionPane.showMessageDialog(
null, "Please Fill All Fields to add Record.”);
} else {
String sql = "insert into auction" + "(ITEM_NAME,IMAGE,PRICE)" + "values
(?,?,?)";
try {
File f = new File(path.getText());
InputStream inputStream = new FileInputStream(f);
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/intern", "root", "root");
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, nameData.getText());
statement.setBlob(2, inputStream);
statement.setString(3, priceData.getText());
statement.executeUpdate();
JOptionPane.showMessageDialog(null, "DETAILS ADDED SUCCESSFULLY");
nameData.setText("");
priceData.setText("");
imageLabel.setIcon(null);
path.setText("");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
tableData();
}
}
});
SELECTIMAGEButton.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter =
new FileNameExtensionFilter("*.IMAGE", "jpg", "png");
fileChooser.addChoosableFileFilter(filter);
int rs = fileChooser.showSaveDialog(null);
if (rs == JFileChooser.APPROVE_OPTION) {
File selectedImage = fileChooser.getSelectedFile();
path.setText(selectedImage.getAbsolutePath());
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
imageLabel.setIcon(resize(path.getText()));
}
}
});
table1.addMouseListener(new MouseAdapter() {
public
void startTimer() {
timer = new Timer(
1000, new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
sec--;
if (sec == -1) {
timer.stop();
tableData();
} else if (sec >= 0 && sec < 10)
timerLabel.setText("00:0" + sec);
else
timerLabel.setText("00:" + sec);
}
});
}
public
void tableData() {
try {
String a = "Select* from auction";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/intern", "root", "root");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(a);
table1.setModel(buildTableModel(rs));
} catch (Exception ex1) {
JOptionPane.showMessageDialog(null, ex1.getMessage());
}
}
public
static DefaultTableModel buildTableModel(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output :
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
4. Learning Outcomes :
4. Learnt about Client-side and Server-side form validation to ensure data integrity.