Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to find out how to access data in SQl server

Status
Not open for further replies.

cal555

Programmer
May 5, 2006
71
US
Hi, my commpany going to java, currently I am a VB programer, so I am trying to learn Java. We do a lot with Access adn SQL Server databases, sofar I found that the code below works great for Ms Access, but how would I do it if it where a table in SQL Server I wnted to connect to.
Can someone tell me how I would do this, or tell me where I can go get this information; because I have searched the net and cannot find it.

Thank You

import java.sql.*;
public class Test2
{
public static void main(String[] args)
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String filename = "C:/Orders.mdb";
String database = "jdbc:eek:dbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="; database+= filename.trim() + ";DriverID=22;READONLY=true}";

Connection con = DriverManager.getConnection( database ,"","");

Statement s = con.createStatement();
s.execute("Update TEST12345 Set column_name = 4");
s.execute("select column_name from TEST12345");
ResultSet rs = s.getResultSet();
if (rs != null)
while ( rs.next() )
{
System.out.println("Data from column_name: " + rs.getString(1) );
}
s.close();
con.close();
}
catch (Exception err) {
System.out.println("ERROR: " + err);
}
}
}
 
i found jtds driver easy to use.
Download the driver from and install into your java library search path. [I copied the jtds.jar into java_home/jre/lib/ext]

Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:jtds:sqlserver://host/database_name;user=sa;password=?");
 
Do not copy the driver jars to the Java installation ext directory. Put them with your application (for example, in a 'lib' subfolder) and include it in the regular classpath when executing it.

Tim
 
Why is it wrong to copy the driver jars to java installation ext directory?
 
It's like using JNI and put the dlls on the system32 folder of windows.

If you don't keep appart the platform from the application, how can you maintain it? How will you work when you have 25 application or utility jars in the ext dir?

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top