This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes Error in Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Error in Connection" Watch "Error in Connection" New topic
Author

Error in Connection

ritesh r
Greenhorn

Joined: Jul 19, 2001
Posts: 24
Hi Techies:

I am facing a unique problem where in I have to connect to the Sql Server in the init() method of the JApplet.
But during initialisation,I am getting the below mentioned runtime error.
java.lang.Runtime Exception with
Class.forName()
and DriverManager()

May I know why this error is coming.
Bye
Regards
Ritesh
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

why don't you show me the code where your trying to make
the connection, I will try to help you.
Yoel Stern

------------------
Sun Certified Programmer for JAVA 2 Platform
ritesh r
Greenhorn

Joined: Jul 19, 2001
Posts: 24
Hi Yoel
I have modified the same for your reference only.Now I am able to see the JTable with the data content but unable to refresh the table when I select a combo-box value.Help is urgent.
The source Code:
//Declare the Packages going to be imported here below
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
import java.sql.*;
//<Applet code="JApplicationSql.class" width=500 height=500></Applet>
public class JApplicationSql extends JFrame implements ItemListener
{
//Initialize the Components
//DefaultTableModeldefaulttablemodel=new DefaultTableModel();
JTablejtable;//=newJTable(defaulttablemodel);//A Dynamic Modifiable JTable
Vectorrow,column,row_1,column_1;//No. of rows and columns in a JTable
JLabel label_combo=new JLabel();
JComboBox comboBox =new JComboBox();
JPanel jpanel;
//comboBox.setEditable(true);

//Declare the Container1
Container contentPane;

//Database Declarations
//Connection connection;
//String dbUrl = "jdbc dbc:mydsn";
//String user = "sa";
//String password = "";

private Statement stmt;
private ResultSet rs;
private ResultSetMetaData rmeta;
String combo_value="";
//String row_value="";


//Initialization of Constructor
public JApplicationSql()
{
jpanel=new JPanel();

label_combo.setText("Select the Department");
label_combo.setFont(new Font("Verdana",1,17));

comboBox.addItem(" 10 ");
comboBox.addItem(" 20 ");
comboBox.addItem(" 30 ");
comboBox.addItem(" 40 ");
comboBox.addItem(" 50 ");

//contentPane.setLayout(new FlowLayout());
jpanel.add(label_combo);
jpanel.add(comboBox);
//Add the Listeners
comboBox.addItemListener(this);

contentPane=getContentPane();
contentPane.add(jpanel,BorderLayout.NORTH);

//Load the Drivers and Connection Objects and Establish the connection
try
{
System.out.println("Locating Drivers");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Connecting to Database");
//connection = DriverManager.getConnection(dbUrl,user,password);
Connection connection = DriverManager.getConnection("jdbc dbc:mydsn;UID=sa;PWD=");
System.out.println("Got Connection");
stmt = connection.createStatement();
}
catch (Exception sqlex)
{
System.out.println("Unable to Connect!!");
sqlex.printStackTrace();
}

}
public static void main(String args[])
{
try
{
JApplicationSql app=new JApplicationSql();
app.setSize(350,200);
app.setVisible(true);
}catch(Exception e)
{}
}
public void itemStateChanged(ItemEvent e)
{
try
{
if (e.getStateChange()==ItemEvent.SELECTED)
{
combo_value=(String)comboBox.getSelectedItem();
populate(combo_value);
jtable.repaint();

/*combo_value=(String)comboBox.getSelectedItem();
//int index=(int)comboBox.getSelectedIndex();
System.out.println("Value of the combo is "+combo_value);
String sql_query="SELECT A.DeptId,A.EmpId,A.Name,A.Designation, B.DeptName FROM EmployeeDetails A,Master_Department B where A.DeptId = B.DeptId and A.DeptId ="+combo_value;
//The Resultset Based Sql Query
//rs = stmt.executeQuery("SELECT EmployeeDetails.EmpId,EmployeeDetails.Name,EmployeeDetails.Designation, EmployeeDetails.DeptId,Master_Department.DeptName FROM EmployeeDetails INNER JOIN Master_Department ON EmployeeDetails.DeptId = 'index'");
rs=stmt.executeQuery(sql_query);
rmeta=rs.getMetaData();

//Retrieve the number of columns and rows and add the number to Vectors
column_1= retrieveColumns();

//Row Data
row_1=retrieveRows();
System.out.println(row_1.size());
//Display the ResultSet data in a JTable
DefaultTableModel defaulttablemodel =new DefaultTableModel(row_1,column_1);
jtable =new JTable(defaulttablemodel);
JScrollPane scrolltab=new JScrollPane(jtable);
//jpanel.add(scrolltab);
contentPane.add(scrolltab,BorderLayout.CENTER);
//contentPane.add(jpanel,BorderLayout.CENTER);
//contentPane.validate(true);
jtable.repaint();*/
}
}
catch(Exception SqlE)
{
System.out.println("Error in retrieving");
}
}

//The Column Data
public Vector retrieveColumns()
{
column=new Vector();
try
{
for(int i=1;i<=rmeta.getColumnCount();i++)<br /> column.addElement(rmeta.getColumnName(i));<br /> //System.out.println(column.size());<br /> }<br /> catch(Exception e)<br /> {<br /> System.out.println("The column Exception is :"+e);<br /> }<br /> return column;<br /> }<br /> <br /> //The Row Data<br /> public Vector retrieveRows()<br /> {<br /> row=new Vector();<br /> try<br /> {<br /> while(rs.next())<br /> {<br /> Vector currow=new Vector();//Current Row<br /> for(int i=1;i<=rmeta.getColumnCount();i++)<br /> currow.addElement(rs.getString(i));<br /> System.out.println(currow.size());<br /> row.addElement(currow);<br /> }<br /> }<br /> catch(SQLException se)<br /> {<br /> System.out.println("The Exception is :"+se);<br /> }<br /> return row;<br /> }<br /> public void populate(String val)<br /> {<br /> try<br /> {<br /> System.out.println("Value of the combo is "+val);<br /> String sql_query="SELECT A.DeptId,A.EmpId,A.Name,A.Designation, B.DeptName FROM EmployeeDetails A,Master_Department B where A.DeptId = B.DeptId and A.DeptId ="+val;<br /> <br /> //The Resultset Based Sql Query<br /> //rs = stmt.executeQuery("SELECT EmployeeDetails.EmpId,EmployeeDetails.Name,EmployeeDetails.Designation, EmployeeDetails.DeptId,Master_Department.DeptName FROM EmployeeDetails INNER JOIN Master_Department ON EmployeeDetails.DeptId = 'index'");<br /> rs=stmt.executeQuery(sql_query);<br /> rmeta=rs.getMetaData();<br /> <br /> //Retrieve the number of columns and rows and add the number to Vectors<br /> column_1= retrieveColumns();<br /> <br /> //Row Data<br /> row_1=retrieveRows();<br /> <br /> //Display the ResultSet data in a JTable<br /> DefaultTableModel defaulttablemodel =new DefaultTableModel(row_1,column_1);<br /> jtable =new JTable(defaulttablemodel);<br /> JScrollPane scrolltab=new JScrollPane(jtable);<br /> jtable.setPreferredScrollableViewportSize(new Dimension(500, 70));<br /> //jpanel.add(scrolltab);<br /> contentPane.add(scrolltab,BorderLayout.CENTER);<br /> //contentPane.add(jpanel,BorderLayout.CENTER);<br /> //contentPane.validate(true);<br /> //jtable.repaint();<br /> }<br /> catch(Exception eop){System.out.println(eop);}<br /> }<br /> }<br /> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Yoel..it is very urgent.Why am I unable to refresh the data into a jtable.
Regards
Ritesh
DAYANAND BURAMSHETTY
Ranch Hand

Joined: Aug 06, 2001
Posts: 34

public Connection getConnection() throws SQLException
{
System.out.println("ConnectionTest....................\n");
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
} catch (ClassNotFoundException cnfe) {
throw new SQLException(cnfe.getMessage());
} catch (IllegalAccessException iae) {
throw new SQLException(iae.getMessage());
} catch (InstantiationException ie) {
throw new SQLException(ie.getMessage());
}
Connection con = null;
Properties props = new Properties();
props.put("user","sa");
props.put("password","");
try {
con = DriverManager.getConnection("jdbc:weblogic:mssqlserver4:mydsn@ip_address:1433",props);
return con;
} catch (Exception e) {
System.out.println("Exception :::"+e.getMessage());
throw new SQLException (e.getMessage());
}
}


Dil se....,<BR>Dayanand<BR>0065-8839071(off)<BR>0065-7547034(Res0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error in Connection