Import java.sql.*;
/**
* <P>TITLE:ODBC Connection Database </p>
* <p>description: This example demonstrates how to manipulate a database using the Jdbc-odbc bridge. </p>
* <p>copyright:copyright (c) 2003</p>
* <p>Filename:odbcConn.java</p>
* @version 1.0
*/
public class odbcconn{
private String url= "";
private String username= "";
private String password= "";
/**
*<br> Method Description: Obtain data connection
*<br> input Parameters:
*<br> return Type: Connection Connection object
*/
public Connection Conn () {
try {
class.forname ("Sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = drivermanager.getconnection (URL, username, password);
return con;
}catch (SQLException sqle) {
SYSTEM.OUT.PRINTLN ("Can´t Connection db:" +sqle);
return null;
} catch (Exception e) {
System.out.println ("Failed to load Jdbc/odbc driver.");
return null;
}
}
/**
*<br> Method Description: Execute query SQL statement
*<br> input parameters: Connection Con database connection
*<br> input parameter: String SQL SQL statement to execute
*<br> return type:
*/
public void query (Connection con, String sql) {
try{
if (con==null) return;
Statement stmt = Con.createstatement ();
ResultSet rs = stmt.executequery (SQL);
ResultSetMetaData Rmeta = Rs.getmetadata ();
int numcolumns = Rmeta.getcolumncount ();
while (Rs.next ())
{
for (int i = 0;i< numcolumns;i++)
{
String stemp = rs.getstring (i+1);
System.out.print (stemp+ "");
}
System.out.println ("");
}
}catch (Exception e) {
System.out.println ("Query error:" +e);
}finally{
try{
Con.close ();
}catch (SQLException se) {}
}
}
/**
*<br> Method Description: Perform inserts, updates, deletes, and so on without returning a result set of SQL statements
*<br> input parameters: Connection Con database connection
*<br> input parameter: String SQL SQL statement to execute
*<br> return type:
*/
public void execute (Connection con, String sql) {
try{
if (con==null) return;
Statement stmt = Con.createstatement ();
stmt.executeupdate (SQL);
}catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Query error:" +e);
}finally{
try{
Con.close ();
}catch (SQLException se) {}
}
}
/**
*<br> Method Description: Main method
*<br> input Parameters:
*<br> return type:
*/
public static void Main (string[] arg) {
if (arg.length!=3) {
System.out.println ("Use:java odbcconn URL username password");
return;
}
odbcconn oc = new Odbcconn ();
oc.url = arg[0];
Oc.username=arg[1];
oc.password=arg[2];
Oc.execute (Oc.conn (), insert into userinfo (name,address) VALUES (´switch´,´new York´) ");
Oc.query (Oc.conn (), "SELECT * from UserInfo");
}
}