Connecting Sybase databases in Java (example)

Source: Internet
Author: User
Tags final return string sybase version tostring stringbuffer
Data | database package person.fane.test;
/*
* Date Created 2005-7-15
* Created by Fane
*/


Import java.sql.*;
/**
* A JDBC test application for Sybase
* @author Fane
* @version 1.0.0
* @since JDK1.4
*/
public class Sybasetest {
Private final String sybasedrivername = "Com.sybase.jdbc3.jdbc.SybDriver";
Private final String Sybaseurltoconnect = "jdbc:sybase:tds:192.168.0.201:7543/cpemyy?charset=cp850&jconnect_ Version=3 ";
Private Connection myconnection = null;
/**
* To load the JDBC driver
*
*/
Public Sybasetest ()
{
Try
{
Class.forName (Sybasedrivername);
}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 (Sybaseurltoconnect, "sa", "");

}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) {
Sybasetest mysybasetest = new Sybasetest ();
Try
{
Connection myconnection = Mysybasetest.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 (1) + "");
Mystringbuffer.append (myresultset.getstring (2) + "");
Mystringbuffer.append (myresultset.getstring (3) + "");
Mystringbuffer.append (Myresultset.getstring (4) + "");
Mystringbuffer.append (Myresultset.getint (5) + "");
Mystringbuffer.append (Myresultset.getint (6) + "\ n");
}

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 ());
}

}

}


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.