aspose file tools*
The moose likes Java in General and the fly likes ClassNotFoundException while dealing with Database connectivity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ClassNotFoundException while dealing with Database connectivity" Watch "ClassNotFoundException while dealing with Database connectivity" New topic
Author

ClassNotFoundException while dealing with Database connectivity

vinay abhishek
Greenhorn

Joined: Nov 23, 2012
Posts: 18
Hello Friends,
When I run a Java class that has a Database connectivity in it a ClassNotFoundException is reported. My code goes like this...
/*
* Bill.java
*
*
*
*/

package javaapplication1;



import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.sql.*;
import java.text.MessageFormat;
import java.util.*;
import javax.swing.*;

public class Bill extends JFrame implements ActionListener, WindowListener
{


JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7,jl9,jl10,jl11,jl12,jl13,jl14;
JTextField jt1,jt2,jt3,jt4,jt5,jt6,jt7,jt8,jt9;
JButton jb1,jb2,jb3;
Container cp;
JComboBox jc1,jc2,jc3,jc4;
int item_id=0,b=0;
int unit=0;
int sum=0;
Statement st1;
Font f1;
String cust=null;
String ptype=null;
int custidd=0;
public Bill()

{
super("BILL");

try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}

catch(Exception ee)
{

}

cp=this.getContentPane();
cp.setBackground(Color.cyan);
f1=new Font("Arial Black",Font.BOLD,14);
jl1=new JLabel("Bill Invoice");
jl1.setFont(f1);
jl1.setForeground(Color.RED);
jl2=new JLabel("Western Michigan University");
jl3=new JLabel("DATE");
jl4=new JLabel("TIME");
jl5=new JLabel("VAT AMT 4%");
jl6=new JLabel("TOTAL");
jl13= new JLabel("GRAND TOTAL");
jt9= new JTextField(10);
jl7=new JLabel("Telephone no");
jt8=new JTextField("908-267-0999");
jt8.setEditable(false);
jl9=new JLabel("Category");
jl14=new JLabel("Cust_id");
jl10=new JLabel("Bill No.");
jb1=new JButton("BACK");
jb2=new JButton("PRINT");
jt1=new JTextField();
jl12=new JLabel(" PAYMENT TYPE");
jc1=new JComboBox();
jc2=new JComboBox();
jc4=new JComboBox();

jc1.addItem("cash");
jc1.addItem("credit");
jc2.addItem("Customer");
jc2.addItem("Reg Cust");


String title[]=new String[]{"item name ","company name", "weight","price per unit", "no of unit","total price"};
Object [][] data=new Object[50][6];
final JTable jt=new JTable(data,title);

int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
JScrollPane jsp=new JScrollPane(jt,v,h);
jsp.setBounds(50,200,600,300);
getContentPane().add(jsp);

GregorianCalendar cal= new GregorianCalendar();

jt1.setText(""+cal.get(Calendar.DATE)+"/"+cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.YEAR));
jt1.setEditable(false);
jt2=new JTextField();
jt2.setText(cal.get(Calendar.HOUR)+ ":"+cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND));
jt2.setEditable(false);
jt3=new JTextField(10);
jt4=new JTextField(10);
jt5=new JTextField(10);
jt6=new JTextField(10);


jl11=new JLabel();
jl11.setBorder(BorderFactory.createLineBorder(Color.BLACK,5));
jl11.setBounds(2,2,850,755);
cp.add(jl11);



cp.add(jl1);
cp.add(jl2);
cp.add(jl3);
cp.add(jl4);
cp.add(jl5);
cp.add(jl6);
cp.add(jl7);
cp.add(jt8);
cp.add(jl9);
cp.add(jl12);
cp.add(jl14);
cp.add(jc4);
cp.add(jl10);
cp.add(jb1);
cp.add(jb2);
cp.add(jt1);
cp.add(jt2);
cp.add(jt3);
cp.add(jt4);
cp.add(jt5);
cp.add(jt6);
cp.add(jc1);
cp.add(jc2);

cp.add(jl13);
cp.add(jt9);

cp.setLayout(null);

jl1.setBounds(350,10,200,50);
jl2.setBounds(350,30,200,50);
jl3.setBounds(250,150,50,50);
jl4.setBounds(450,150,50,50);
jl6.setBounds(430,500,100,50);
jl5.setBounds(430,550,100,50);
jl7.setBounds(580,70,70,30);
jl9.setBounds(10,50,100,50);
jl14.setBounds(10,90,100,30);
jt8.setBounds(650,70,100,30);
jl12.setBounds(10,520,100,50);
jl13.setBounds(430,600,100,50);
jl10.setBounds(10,140,100,50);
jb1.setBounds(150,620,100,30);
jb2.setBounds(300,620,100,30);

jc1.setBounds(100,520,100,50);
jc2.setBounds(100,50,100,30);
jc4.setBounds(100,90,100,30);
jt1.setBounds(300,150,100,30);
jt2.setBounds(500,150,100,30);
jt4.setBounds(540,500,100,30);
jt3.setBounds(540,550,100,30);
jt9.setBounds(540,600,100,30);
jt6.setBounds(100,140,100,35);
jb1.addActionListener(this);
jb2.addActionListener(this);

jc4.setVisible(false);
jl14.setVisible(false);


jc4.addActionListener(new ActionListener()
{


public void actionPerformed(ActionEvent ae)
{

custidd=Integer.parseInt((String) jc4.getSelectedItem());

}
});


try
{
Class.forName("com.mysql.jdbc.Driver");
Connection c1 = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "myroot123");
st1=c1.createStatement();

ResultSet rs1=st1.executeQuery("select * from customer ");
while(rs1.next())

{
int custid=rs1.getInt("cust_id");
jc4.addItem(""+custid);
}

}
catch(Exception eee)
{


}




addWindowListener(this);

try
{
Class.forName("com.mysql.jdbc.Driver");
Connection c1 = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "myroot123");
st1=c1.createStatement();

ResultSet rs1=st1.executeQuery("select * from temptable ");
ResultSet rs2;

int f=0;
int gtotal=0;
while(rs1.next())
{

String b2=rs1.getString("itemname");
String b3=rs1.getString("coname");
String weight=rs1.getString("weight");
int mrp=Integer.parseInt(rs1.getString("mrp"));
unit=Integer.parseInt(rs1.getString("unit"));
int total=Integer.parseInt(rs1.getString("total"));

gtotal=gtotal+total;

jt.setValueAt(b2,f,0);
jt.setValueAt(b3,f,1);
jt.setValueAt(weight,f,2);
jt.setValueAt(mrp,f,3);
jt.setValueAt(unit,f,4);
jt.setValueAt(total,f,5);

f++;

}

jt4.setText(""+gtotal);
int v1=gtotal*4/100;

jt3.setText(""+v1);
jt9.setText(""+(v1+gtotal));

}
catch(Exception eeee)
{
JOptionPane.showMessageDialog(null,"exception is"+eeee);
}

try
{
Class.forName("com.mysql.jdbc.Driver");
Connection c1 = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "myroot123");
Statement st1=c1.createStatement();

ResultSet rs1=st1.executeQuery("select billno from Bill1");

String bno=null;
while(rs1.next())
{
bno=rs1.getString("billno");

}
b=Integer.parseInt(bno);
b++;
jt6.setText(""+b);


}
catch(Exception ee)
{

JOptionPane.showMessageDialog(null,"exception is"+ee);
}


final MessageFormat header=new MessageFormat("Page{0}");

final MessageFormat footer=new MessageFormat("- {0} -");








jc1.addActionListener(new ActionListener()
{


public void actionPerformed(ActionEvent ae)
{

cust=(String) jc1.getSelectedItem();
if(cust.equals("credit") &&ptype.equals("Reg Cust") )
{

cuacc cu=new cuacc();
cu.setSize(600,600);
cu.setVisible(true);
}
// JOptionPane.showMessageDialog(null,"this is"+cust) ;

} });

jc2.addActionListener(new ActionListener()
{


public void actionPerformed(ActionEvent ae)
{

ptype=(String) jc2.getSelectedItem();


jc4.setVisible(false);
jl14.setVisible(false);
if(ptype.equals("Reg Cust"))

{
jc4.setVisible(true);
jl14.setVisible(true);
}

// JOptionPane.showMessageDialog(null,"this is"+ptype) ;

} });



jb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try{
jt.print(JTable.PrintMode.FIT_WIDTH,header,footer);

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "myroot123");
Statement stmt=con.createStatement();


String date=jt1.getText();
int total=Integer.parseInt(jt9.getText());


int r=stmt.executeUpdate("insert into Bill1 values ('"+b+"',"+custidd+",'"+date+"',"+total+",'"+ptype+"')");

}

catch(PrinterException pe)
{
JOptionPane.showMessageDialog(null,"Printer error"+pe);
}
catch(Exception eee)
{
JOptionPane.showMessageDialog(null,"exception"+eee);
}

try{

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "myroot123");
Statement stmt=con.createStatement();
ResultSet rs1=stmt.executeQuery("select * from temptable ");
int it=0;
while(rs1.next())
{
int unitt=0;
int u1=rs1.getInt("unit");
it=rs1.getInt("itemcode");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con1 = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "myroot123");
Statement stmt1=con1.createStatement();
ResultSet rs2=stmt1.executeQuery("select unit from stockupdation where item_id="+it+"");


while(rs2.next())
{
unitt=rs2.getInt("unit");

}
}
catch(Exception ee)
{

JOptionPane.showMessageDialog(null,"exception iss"+ee);
}



unitt=unitt-u1;
JOptionPane.showMessageDialog(null,"unit is"+unitt);
Class.forName("com.mysql.jdbc.Driver");
Connection con2 = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "myroot123");
Statement stmt2=con2.createStatement();
int rr=stmt2.executeUpdate("update stockupdation set unit="+unitt+" where item_id="+it+" ");

}
}
catch(Exception ee)
{
JOptionPane.showMessageDialog(null,"exception isss"+ee);

}
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "myroot123");
Statement st1=c.createStatement();

st1.executeUpdate("delete * from temptable");
}

catch(Exception ee)
{

JOptionPane.showMessageDialog(null,"exception has occured"+ee);

}


}
}
);

setSize(500,400);
setVisible(true);

}
private void initComponents()
{
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 300, Short.MAX_VALUE)
);
pack();
}


public void actionPerformed(ActionEvent aeee)
{
String s=aeee.getActionCommand();

if(s.equals("BACK"))
{
purchaseentry pe=new purchaseentry();
pe.show();
pe.setSize(700,700);
pe.setVisible(true);
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "myroot123");
Statement stmt=con.createStatement();
int r=stmt.executeUpdate("delete from temptable");

this.dispose();
}
catch(Exception ee)
{
JOptionPane.showMessageDialog(null,"exception occured"+ee);
}

}

}

public void windowDeactivated(WindowEvent we)
{



}

public void windowActivated(WindowEvent we)
{



}
public void windowDeiconified(WindowEvent we)
{



}

public void windowIconified(WindowEvent we)
{


}


public void windowClosing(WindowEvent we)
{

try
{
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "myroot123");
Statement st1=c.createStatement();

st1.executeUpdate("delete * from temptable");
}

catch(Exception ee)
{

JOptionPane.showMessageDialog(null,"exception has occured"+ee);

}

}
public void windowOpened(WindowEvent we)
{



}



public void windowClosed(WindowEvent e)
{


}

public static void main(String args[]) {

Bill bl=new Bill();
bl.setBounds(40,2,800,692);
bl.setVisible(true);

}


}
See the areas where there is database connectivity. I am not able to find a way out. I am a java beginner
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Please UseCodeTags (<-click) when posting the code you want to point out, so it will be easier to read and you will most likely get better answers. With code like this, many won't even try to go through the code. Also make sure when posting a question to TellTheDetails and ShowSomeEffort. In your case, it would be better if you had included complete exception message with original post and just part of code not the entire class(es). So, let's start with it and see that exception message.

I am a java beginner

This statement is contradictory with the fact that you're using Swing and trying to establish connection to database.


The quieter you are, the more you are able to hear.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
You would have to provide a lot more details, including the full error message, where the class is supposed to be which you can’t find and the -cp option you are using.
vinay abhishek
Greenhorn

Joined: Nov 23, 2012
Posts: 18
I apologize for the way I presented my question. I respect all the prompt replies and I assure every one that I would get back after framing my question properly
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassNotFoundException while dealing with Database connectivity