Question:

How can I display the data on an Internet page in tabular format using java? Data I retrieved from SQL?

by  |  earlier

0 LIKES UnLike

Hi.

I created an application using java and have SQL server as backend to store the data. The application can get the input from user and put it on sql database and retrieving a single record is possible in it.

Now, actually I want that whenever a user click on a button say "View all records" then all the data from sql should be displayed in tabular format on a new web page. I get the resultset from the backend but not able to display it on a web page and dont know how to do this.

Also, note that this is an application not an applet here. I want to display the data retrieved from backend on a webpage.

Here is my code:

==========================

Viewrec.java:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.sql.*;

import java.util.Date;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

public class Viewrec implements ActionListener

{

JFrame f;

JPanel p;

JLabel l1, Head;

JButton btViewrec, btback;

public static void main(String args[])

{

new Viewrec();

}

public Viewrec()

{

f = new JFrame("View records");

p = new JPanel();

f.getContentPane().add(p);

f.setVisible(true);

f.setSize(800,600);

f.setDefaultCloseOperation(JFrame.EX...

p.setBorder(BorderFactory.createLine... 5));

p.setBackground(Color.white);

p.setLayout(null);

Head=new JLabel("Viewrec Records");

l1=new JLabel("Please click on the following button to get the complete report");

btViewrec= new JButton("View");

btback= new JButton("Back");

Head.setBounds(350, 30, 200,30);

l1.setBounds(50,75,500,20);

btViewrec.setBounds(190,180,100,30);

btback.setBounds(410,180,100,30);

p.add(Head);

p.add(l1);

p.add(btViewrec);

p.add(btback);

btViewrec.addActionListener(this);

btback.addActionListener(this);

}

public void actionPerformed(ActionEvent ev)

{

Object source = ev.getSource();

if(source == btViewrec)

{

searchInfo();

}

if(source == btback)

{

new Welcome();

this.f.setVisible(false);

}

}

public void searchInfo()

{

new testing();

this.f.setVisible(true);

}

}

=============================

testing.java-- this is carrying the data on a new panel

import java.awt.*;

import java.io.*;

import java.sql.*;

import java.util.*;

import javax.swing.*;

import javax.swing.table.*;

public class testing

{

JFrame f;

JPanel p;

JScrollPane scrollPane;

public testing()

{

f=new JFrame("Showing all records");

p=new JPanel();

f.setDefaultCloseOperation(JFrame.EXI...

f.pack();

f.setVisible(true);

f.getContentPane().add(p);

p.setBackground(Color.white);

Vector columnNames = new Vector();

Vector data = new Vector();

try

{

Connection con=DriverManager.getConnection("jdbc:od...

// Read data from a table

String sql = "select * from candidate";

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(sql);

ResultSetMetaData md = rs.getMetaData();

int columns = md.getColumnCount();

// Get column names

for (int i = 1; i <= columns; i++)

{

columnNames.addElement(md.getColumnName(...

}

// Get row data

while (rs.next())

{

Vector row = new Vector(columns);

for (int i = 1; i <= columns; i++)

{

row.addElement( rs.getObject(i) );

}

data.addElement( row );

}

rs.close();

stmt.close();

}

catch(Exception e)

{

System.out.println( e );

}

// Create table with database data

JTable table = new JTable(data, columnNames);

scrollPane = new JScrollPane(table);

p.add(scrollPane);

}

public static void main(String[] args)

{

new testing();

}

}

====================================

now, you can see that data is displaying, but let me know how to display it on an Internet page.

Thanks in advance!

Ravi.

 Tags:

   Report

1 ANSWERS


  1. if you are making a web application try using jsp and use HTml at view level..

    what u need to do is pass the data to the web page ( which u would like to make as jsp file).. using dispatcher... it does not seems that this code is passing any data to a web page..  u can either pass the result set or array list or vectors.. like u r doing

    its tough to give the exact code or make u understand it... before you do anything you need to be clear.. what are you trying to do .( which actually i am not..  it seems to me that ur front end..(view level ) is also in java.. which i think should be in html ( or jsp ) for a web based application...!!!

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.