Skip to main content

how to print ms access database report

No replies
mozum
Offline
Joined: 2012-05-01
Points: 0

i am trying to print a report from ms access database report by clicking a jbutton that will send the record to printer job but could not do so. please can any one help me.

Please find below codes for the design of the interface for inserting and retrieving data to be printed from the database.

package mozumm;

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
class Mozum2 extends JFrame
implements ActionListener
{
JLabel lab1 = new JLabel("S.NO");
JTextField f1 = new JTextField(10);
JLabel lab2 = new JLabel("Title");
JTextField f2 = new JTextField(10);
JLabel lab3 = new JLabel("Author");
JTextField f3 = new JTextField(10);
JButton sub = new JButton("Submit");
JButton ex = new JButton("Exit");
JButton print= new JButton("Print");
public static void main(String[] args)
{
Mozum2 mz = new Mozum2();
try{
String host = "jdbc:mysql://localhost:3306/test";
String uName = "admin";
String uPass = "";
Connection con = DriverManager.getConnection(host,uName,uPass);
Statement st = con.createStatement();
String SQl = "SELECT * FROM counselor";
ResultSet rs = st.executeQuery(SQl);
while(rs.next())
{
int counselor_id = rs.getInt("COUNSELOR_ID");
String first_name = rs.getString("FIRST_NAME");
String nick_name = rs.getString("NICK_NAME");
String last_name = rs.getString("LAST_NAME");
String p = counselor_id + " " + first_name + " " + nick_name + " " + last_name;
System.out.println(p);

}
}
catch(SQLException err){
System.out.println(err.getMessage());
}
}
public Mozum2()
{
setLayout(new FlowLayout());
setSize(200,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.PINK);
JPanel pn = new JPanel();
pn.add(lab1);
pn.add(f1);
add("West", pn);

JPanel pn1 = new JPanel();
pn1.add(lab2);
pn1.add(f2);
add("West", pn1);

JPanel pn2 = new JPanel();
pn2.add(lab3);
pn2.add(f3);
add("West", pn2);

JPanel pn3 = new JPanel();
pn3.add(sub);
pn3.add(ex);
add("West", pn3);

JPanel pn4 = new JPanel();
pn4.add(print);
add("West",pn4);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == sub)
try{
String host = "jdbc:mysql://localhost:3306/test";
String uName = "admin";
String uPass = "";
Connection con = DriverManager.getConnection(host,uName,uPass);
Statement stmt = con.createStatement();
String first_name = f1.getText();
String nick_name = f2.getText();
String last_name = f3.getText();
String sql="Insert into counselor values('"+(first_name)+"','"
+(nick_name)+"','"+(last_name)+"')";
stmt.executeUpdate(sql);
}
catch(SQLException err){
JOptionPane.showMessageDialog(this, err.getMessage());
}
}
}