• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java-Xml file not getting Connected to DataBase

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My java program which parses an xml file for connecting to MySql database shows an error when i tried to run it.The error is

java.lang.ClassNotFoundException: driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:140)
at JAXP1.main(JAXP1.java:50)

The XML file is as shown below.

<dbconnection>


<username> abc </username>
<password> abc123 </password>

<url> jdbc:mysql://192.168.1.83/abc </url>
<driver> com.mysql.jdbc.Driver </driver>

</dbconnection>

and the java code is


import java.sql.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.IOException;
import java.util.StringTokenizer;
public class JAXP1
{
static Connection con=null;
static Statement stmt=null;
static ResultSet resultset;
static String username=" ";
static String password=" ";
static String url=" ";
static String driver=" ";
static String s=" ";
static String ss=" ";


public static void main(String[] args)
{
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
JAXP1 counter = new JAXP1();

for (int i = 0; i < args.length; i++)
{
try
{
Document d = parser.parse(args[i]);
display(d);
}
catch (SAXException e)
{
System.err.println(e);
}
catch (IOException e)
{
System.err.println(e);
}
}
try
{
System.out.println("Driver " + driver);
System.out.println("URL "+url);
System.out.println("user"+username +", password"+ password);
Class.forName(driver).newInstance();
con=DriverManager.getConnection(url,username,password);
stmt=con.createStatement();
if(con.isClosed())
System.out.println("Connected");
else
System.out.println("Not Connected");
}
catch(Exception e)
{
e.printStackTrace();

}


}
catch (ParserConfigurationException e)
{
System.err.println(e);
}


}

public static void display(Node node)
{
if (node.hasChildNodes())
{
NodeList children = node.getChildNodes();
for(int i=0;i<children.getLength();i++)
{

ss=node.getNodeName();
if(ss.equals("username"))
{
username =ss;
}
if(ss.equals("password"))
{
password =ss;
}

if(ss.equals("url"))
{
url =ss;
}
if(ss.equals("driver"))
{
driver =ss;
}

System.out.println("Tag Name= "+ss);
display(children.item(i));
}

}
int type = node.getNodeType();
NodeList child = node.getChildNodes();
if(type==Node.TEXT_NODE)
{
s=node.getNodeValue();
for (int i=0; i < child.getLength(); i++)
{
s=node.getNodeValue();
if(ss.equals("username"))
{
username = s;
}
if(ss.equals("password"))
{
password = s;
}
if(ss.equals("url"))
{
url = s;
}
if(ss.equals("driver"))
{
driver = s;
}

}
/*try
{
Class.forName(driver).newInstance();
con=DriverManager.getConnection(url,username,password);
stmt=con.createStatement();
if(con.isClosed())
System.out.println("Connected");
else
System.out.println("Not Connected");
}
catch(Exception e)
{
e.printStackTrace();

}
}*/
System.out.println(ss+s);

}
}

}

I am not getting connected to the data base.

could any one help me....?
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>System.out.println("Driver " + driver);
At this javacode, does it print Driver com.mysql.jdbc.Driver ??
Are you able to connect to database by directly supplying the database connection strings ??
 
J Abraham
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.I am getting the name 'Driver' printed.But the value 'com.mysql.jdbc.Driver' is not getting printed.The same is for username,password,url.
should i change the code ? is this a logical problem ?
 
J Abraham
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am getting the following output.

Tag Name= #document
Tag Name= dbconnection
dbconnection



Tag Name= dbconnection
Tag Name= username
username abc
Tag Name= dbconnection
dbconnection

Tag Name= dbconnection
Tag Name= password
password abc123
Tag Name= dbconnection
dbconnection


Tag Name= dbconnection
Tag Name= url
url jdbc:mysql://192.168.1.83/abc
Tag Name= dbconnection
dbconnection

Tag Name= dbconnection
Tag Name= driver
driver com.mysql.jdbc.Driver
Tag Name= dbconnection
dbconnection


Driver driver
URL url
userusername, passwordpassword
java.lang.ClassNotFoundException: driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:140)
at JAXP1.main(JAXP1.java:50)

hope you could help me.
 
Balaji Loganathan
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jibin J Abraham:
i am getting the following output.
Tag Name= dbconnection
Tag Name= driver
driver com.mysql.jdbc.Driver
Tag Name= dbconnection
dbconnection


Driver driver
URL url
userusername, passwordpassword

hope you could help me.



As you can see, its not storing driver url in the variable "Driver", so you have to re-code your parsing fucntion to retrieve driver tag value and store them. IBM developer works have some tutorial on "Understaing DOM and SAX" check it out as well to learn about xml parsing
reply
    Bookmark Topic Watch Topic
  • New Topic