Friday, June 30, 2006

[java] Shows usage of executeQuery(SQL)

Shows usage of executeQuery(SQL)

import java.sql.*;

import java.util.*;

public class StatementApp extends java.lang.Object {

/** Creates new StatementApp */

public StatementApp() {

}

/**

* @param args the command line arguments

*/

public static void main (String args[]) {

if (args.length!=1) {

System.out.println("Usage: java StatementApp sql");

System.exit(0);

}

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = DriverManager.getConnection("jdbc:odbc:test", "sa", "");

String sql = args[0];

System.out.println(sql);

Statement stat=con.createStatement();

ResultSet res =stat.executeQuery(sql);

while(res.next())

{

System.out.println(res.getString(2));

}

con.close();

}

catch(Exception e)

{

System.out.println(e);

System.exit(0);

}

}

static void displayResults(ResultSet r) throws SQLException

{

ResultSetMetaData rmeta = r.getMetaData();

int numColumns = rmeta.getColumnCount();

for (int i=0; i<=numColumns; ++i)

{

if (i<numColumns)

System.out.print(rmeta.getColumnName(i)+" | ");

else

System.out.println(rmeta.getColumnName(i));

}

while(r.next())

{

for (int i=0; i<=numColumns; ++i)

{

if (i<numColumns)

System.out.print(r.getString(i)+" | ");

else

System.out.println(r.getString(i).trim());

}

}

}

}

Pradyut
http://pradyut.tk
http://spaces.msn.com/members/oop-edge/
http://groups-beta.google.com/group/oop_programming
India

No comments: