Results 1 to 20 of 21
Thread: how to configure DSN....??
- 05-17-2010, 06:15 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 28
- Rep Power
- 0
how to configure DSN....??
good morning ppl
i have my data base in SQL Server 2008 and this data base is remote data base i have tried connecting to the data base using DSN in a window machine its runs successfully and the code given below is the same code which works on window machine using DSN!!!
can any one help me out if i want to deploy the same class in Linux Redhat and how to configure DSN in Linux Redhat!!!
The Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
public class ABC
{
public static Connection con;
public static PreparedStatement ps;
public static ResultSet rs;
public static void main(String[] arg)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:<DSN REFRENCE>","<USER NAME>","<PASSWORD>");
ps = con.prepareStatement("Select * from <DATABASE NAME>.dbo.<TABLE NAME> where ClientUserId=? and ClientPasswd=?");
ps.setString(1,"swapnil");
ps.setString(2,"swapnil");
rs=ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
if(rs.next())
{
for(int i = 1;i <=rsmd.getColumnCount();i++)
{
System.out.println(rs.getString(i));
}
System.out.println("Login Successfull..");
}
else
System.out.println("Login Unsuccessful...");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
this code runs successfully on window machine but when the same code is compiled on Redhat there is no error while creating its object file but since i have no DNS the following error is returned!!!!!
The Error:
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
so can any one help me how to Configure DSN in Linux Redhat???
- 05-17-2010, 01:24 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
That has nothing to do with a DSN. That has to do with the fact that you are using GCJ (Gnu Java). Download and install a proper JRE (and make sure to change the /usr/bin/java link and other java related links.
However, you would stll need to have the ODBC Driver for Linux for SQL Server installed (somehow I doubt that you do). You would be much, much, much better off using the SQL Server JDBC Driver (either one downloaded from MS, although I have heard it is not completely compliant, or at least has problems, or, even better, google for and download the jTDS Driver for SQL Server).
- 05-17-2010, 01:52 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 28
- Rep Power
- 0
my other java programs are running on on the machine!!
including jakarta-tomcat-5.0.28
- 05-17-2010, 02:43 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
And? That means nothing. You are trying to use the JDBC-ODBC Bridge here, and that is not included (at least not under that class name) in GCJ. And, that also does not mean that "those other applications" are using the Bridge (or the GCJ for that matter), or that you have any ODBC Drivers installed. I fail to see your point.
- 05-17-2010, 02:45 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 28
- Rep Power
- 0
pls tell me wat exactly i have to do??
again!!
- 05-17-2010, 02:49 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
How are you executing your class.
- 05-17-2010, 02:57 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
What, exactly, you want to do, is to download and use the JDBC Driver, and not the JDBC-ODBC Bridge. If you want to use the Bridge, then you need to install the SQL Server ODBC Driver on your Linux machine (and there is probably a large license cost for it). Then, you also need to make sure that you are using a real JRE (not GCJ). the easiest way to do that is to make sure that one is installed then change all of the /usr/bin links that have anything to do with java.
Edit: Using the JDBC Driver means, of course, changing the Class.forName line and the URL and making sure to include that Driver on the classpath.Last edited by masijade; 05-17-2010 at 03:00 PM.
- 05-18-2010, 05:38 AM #8
Member
- Join Date
- Mar 2010
- Posts
- 28
- Rep Power
- 0
gcj --main=ABC -o ABC ABC.java
this is the command i use to compile java code!!
and i run the program using this command ./ABC
i have already install java in /usr/java/jdk1.6.0_20
and also pls tell me how a java program can be run without using GCJ!
- 05-18-2010, 08:38 AM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Uhm, by actually using the Java you have installed, maybe.
to compile the file and thenJava Code:/usr/java/jdk1.6.0_20/bin/javac ABC.java
to execute the file.Java Code:/usr/java/jdk1.6.0_20/jre/bin/java ABC
- 05-18-2010, 10:43 AM #10
Member
- Join Date
- Mar 2010
- Posts
- 28
- Rep Power
- 0
ok fine the code runs on jdk too!!!
but can i ask u some thing will it be better to run program through GCJ ? or using jdk??
because the code is required to give maximum efficiency!!since code has to genrate a real time data!!
rest regarding jdbbc driver i even dont know how and where to get it from and how to use it!!
- 05-18-2010, 10:57 AM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
What have I said all along about (and what have you yourself noticed while) attempting to use GCJ? Don't. Also, you won't be able to do anything truly "real-time" in Java unless you get "Java RealTime" which is a licensed JDK that is not inexpensive. And any program you've written using the normal JDK is almost guaranteed to fail using JavaRT, but not because of the language. Then again, I can about guarantee that you don't understand the implications of the term "real-time" and you simply mean "near-time".
As far as how to get the driver, see this.
And, hopefully, you know how to use the CLASSPATH if you are programming in Java. If not, learn how. That is something every programmer needs to know. As was the rest of the advice given in this thread, so far.
- 05-18-2010, 11:08 AM #12
Member
- Join Date
- Mar 2010
- Posts
- 28
- Rep Power
- 0
ok let us not consider the case to be real time!!
wat all i need is tat my java code should run most efficiently!!!
regarding class paths if m not soo good at it i will be for sure more i will practice more!
- 05-18-2010, 02:44 PM #13
Member
- Join Date
- Mar 2010
- Posts
- 28
- Rep Power
- 0
ok now i am some how able to set class bath for the driver but still there is an error in the above given code its says java.lanag.NullPointerException!!!!
and after debugging i have found tat Connection Object con has null value!!!!
since i have IP,PORT,USERNAME and PASSWORD
wat will be the Connection String???
- 05-18-2010, 04:11 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
For con to be coming back null I would have expected getConnection() to throw an exception. Are you ignoring exceptions by any chance?
- 05-19-2010, 05:45 AM #15
Member
- Join Date
- Mar 2010
- Posts
- 28
- Rep Power
- 0
@Tolls
bro as u can c in above given code i have kept getConnection() in the Try catch block!!
- 05-19-2010, 07:37 AM #16
Member
- Join Date
- Mar 2010
- Posts
- 28
- Rep Power
- 0
@masijade
i have added jar to /usr/java/jdk1.6.0_20/lib/sqljdbc4.jar
and fired the following command
#export CLASSPATH=/usr/java/jdk1.6.0_20/lib/sqljdbc4.jar
#javac ABC.java
#java ABC
and now i am returned with the following error
Exception in thread "main" java.lang.NoClassDefFoundError: sql
Caused by: java.lang.ClassNotFoundException: sql
at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
Could not find the main class: sql. Program will exit.
i have checked the code in windows machineit is running there!!!
and the new code is now.....
THE CODE:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
public class ABC
{
public static Connection con;
public static PreparedStatement ps;
public static ResultSet rs;
public static void main(String[] arg)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLSer verDriver");
con=DriverManager.getConnection("jdbc:sqlserver://<I.P>:<PORT>","<USER NAME>","<PASSWORD>");
ps = con.prepareStatement("Select * from <DATABASE NAME>.dbo.<TABLE NAME> where ClientUserId=? and ClientPasswd=?");
ps.setString(1,"swapnil");
ps.setString(2,"swapnil");
rs=ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
if(rs.next())
{
for(int i = 1;i <=rsmd.getColumnCount();i++)
{
System.out.println(rs.getString(i));
}
System.out.println("Login Successfull..");
}
else
System.out.println("Login Unsuccessful...");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
- 05-19-2010, 08:16 AM #17
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Well, hopefully you don't actually have a space in the name as shown here "SQLSer verDriver".
And as far as the rest goes, if it turns out to be a simple classpath problem, I am not going to get into trying to explain how to configure and use classpaths properly. That always turns into either a holy war or an exercise in frustation and I am not going through it again.
- 05-19-2010, 09:12 AM #18
Member
- Join Date
- Mar 2010
- Posts
- 28
- Rep Power
- 0
well now i have also figured out the whole problem is about confugring jdk on linux machine and since this is my first time!!!
i have been trying to configure it from last 18 hrs!!
so my friend i dont think u will be as frustated as me!!
- 05-19-2010, 09:18 AM #19
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
But your current problem is about providing the location of your sqljdbc4.jar to your run command.
Use the -cp flag for that.
- 05-19-2010, 09:30 AM #20
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Except that you will only get frustated this once. Once I start getting into "helping" with classpaths again, I will be frustated continously, as they never stop, and they usually take days to resolve properly, as advice is ignored, or muddled because multiple people give differing advice (and much of it wrong, or at least, misadvised), and it is just not worth it. All I can say is, find someone who actually knows it and can stand beside you and explain it to you.
Similar Threads
-
How to configure jmf in net beans
By ishan in forum NetBeansReplies: 0Last Post: 01-08-2010, 06:44 AM -
Configure Apache in netbeans
By mecrazycoder in forum New To JavaReplies: 0Last Post: 07-01-2009, 07:20 AM -
Can we configure MS-SQL server in Netbeans?
By makpandian in forum NetBeansReplies: 0Last Post: 05-19-2009, 07:42 AM -
configure smslib in Eclipse
By javanese in forum EclipseReplies: 1Last Post: 03-01-2009, 04:49 PM -
Configure Classpath
By Doctor Cactus in forum New To JavaReplies: 3Last Post: 01-08-2009, 01:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks