wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes Java-Xml file not getting Connected to DataBase Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Java-Xml file not getting Connected to DataBase " Watch "Java-Xml file not getting Connected to DataBase " New topic
Author

Java-Xml file not getting Connected to DataBase

J Abraham
Ranch Hand

Joined: Jun 25, 2004
Posts: 101
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....?


<b>J Abraham</b>
SCJP5,SCWCD1.4
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
>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 ??


Spritle Software Blogs
J Abraham
Ranch Hand

Joined: Jun 25, 2004
Posts: 101
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

Joined: Jun 25, 2004
Posts: 101
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
Bartender

Joined: Jul 13, 2001
Posts: 3150
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java-Xml file not getting Connected to DataBase
 
Similar Threads
Need Help
Need a help wilh MSAccess
java.sql.SQLException: No suitable driver
SQL server weird connection problem-Pls help