Connecting Oracle Databases in Java (example)

Source: Internet
Author: User
Tags final return string tostring stringbuffer
oracle| Data | database * TODO to change the template for this generated file, go to the
* Windows-Preferences-Java-code styles-code templates
*/
Package person.fane.test;

Import java.sql.*;
/**
* A JDBC test application for Oracle
* @author Fane
* @version 1.0.0
* @since JDK1.4
*/
public class Oracletest {
Private final String oracledrivername = "Oracle.jdbc.driver.OracleDriver";

The following test is used for the table space in Oracle
Private final String Oracleurltoconnect = "Jdbc:oracle:thin:@192.168.0.36:1521:test";
Private Connection myconnection = null;
/**
* To load the JDBC driver
*
*/
Public Oracletest ()
{
Try
{
Class.forName (Oracledrivername);
}catch (ClassNotFoundException ex)
{
System.out.println (ex, "The Driver loaded error,please contacts to your Software designer!"). ToString ());
}
}

Public StringBuffer geterrormessage (Exception ex,string alarmmessage)
{
StringBuffer errorstringbuffer = new StringBuffer ();
Errorstringbuffer.append (Alarmmessage);
Errorstringbuffer.append (Ex.getmessage ());
return errorstringbuffer;
}

/**
* Getconnection method
* @return Connection
*/
Public Connection getconnection ()
{
Try
{
This.myconnection = Drivermanager.getconnection (Oracleurltoconnect, "Fane", "201");

}catch (Exception ex)
{
System.out.println (ex, "Can not get connection,please contacts to your Software designer!"). ToString ());
}

return this.myconnection;

}

/**
* @param args
*/
public static void Main (string[] args) {
Oracletest myoracletest = new Oracletest ();
Try
{
Connection myconnection = Myoracletest.getconnection ();

The SYSTEM.OUT.PRINTLN ("Now begin to Excute ..."); and.

PreparedStatement mypreparedstatement = myconnection.preparestatement ("Select area_id, Area_name,ip_address,tel, area_type,pc_id from C_area_info ");
Mypreparedstatement.setint (1,2);
ResultSet Myresultset = Mypreparedstatement.executequery ();
StringBuffer mystringbuffer = new StringBuffer ();

while (Myresultset.next ())
{

Mystringbuffer.append (Myresultset.getint ("area_id") + "");
Mystringbuffer.append (myresultset.getstring ("area_name") + "");
Mystringbuffer.append (myresultset.getstring ("ip_address") + "");
Mystringbuffer.append (Myresultset.getstring ("tel") + "");
Mystringbuffer.append (Myresultset.getint ("Area_type") + "");
Mystringbuffer.append (Myresultset.getint ("pc_id") + "\ n");
}
System.out.println (Mystringbuffer.tostring ());
System.out.println (New String (Mystringbuffer.tostring (). GetBytes ("Iso-8859-1"), "GBK"));
}catch (Exception ex)
{
System.out.println (ex, "Application Error,please contacts to your Software designer!"). ToString ());
}

}

}




Related Article

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.