[JAVA100 example]052, using ODBC to connect to a database

Source: Internet
Author: User
Tags odbc odbc connection stmt

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");


 }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.