JDBC Connection to Oracle database small demo

Source: Internet
Author: User
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;




public class Javaconnectoracle
{
private static ResultSet rs = null;

private static PreparedStatement PS = null;

private static Connection conn = null;

private static String username = "Scott";

private static String password = "Tiger";


public static void Main (string[] args) throws SQLException
{
Try
{
Class.forName ("Oracle.jdbc.driver.OracleDriver"); Load Drive
}
catch (ClassNotFoundException e)
{
SYSTEM.OUT.PRINTLN ("registration failed");
}

Try
{

ORCL is the database name username is the database user password is the database user's password 1521 is the port number @localhost is the Betty host
conn = Drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:orcl", Username,password);
}
catch (SQLException e)
{
SYSTEM.OUT.PRINTLN ("Establish connection failed");

}

Try
{
PS = conn.preparestatement ("SELECT * from EMP"); Note that the SQL statement here has no semicolon ...


}
catch (SQLException e)
{
E.printstacktrace ();
}


Try
{
rs = Ps.executequery ();
}

catch (Exception e)
{
System.out.println ("AAA");
}



System.out.println ("number \ t name \ t wages");

while (Rs.next ())
{
System.out.println (rs.getstring ("EMPNO") + "T" + rs.getstring ("ename") + "T" + rs.getstring ("SAL"));
}




if (null!= RS)
{
Rs.close ();
}
if (null!= PS)
{
Ps.close ();
}

if (NULL!= conn)
{
Conn.close ();
}






}

}

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.