The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

sql connection and java programming

Posted by swapnil on November 29, 2000 at 1:58 AM

d/muhammad,
hello,
i assume that u have already created a dsn for the sql server database. here the dsn is assumed to be "mydsn_name". Also assumed that u have a table in the database. here the table name is "yourtablename". now pl. find the code for the sql database connection thru the java code .
...............................................................


import java.sql.*;
public class dataconnect
{
public static void main(String[] args)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:mydsn_name");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("Select * from yourtablename");
ResultSetMetaData rsmd=getMetaData();
columnwidth=rsmd.getColumnWidth();
while (rs.next())
{
for (int i=1;i<=columnwidth ;i++ )
{
System.out.print(rs.getString(1) +"\t");
}
System.out.println();

}
}
catch(Exception e)
{System.out.println(e);}

}
}

................................................................

hope this helps,
regards,
swapnil





Replies:
  • database azs February 17, 2001 at 11:16 PM (0)

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us