Java-jdbc-oracle Database Connection

Source: Internet
Author: User

JAVA-JDBC connecting Oracle databases, statement and PreparedStatement comparisons (querying query)

1. PreparedStatement Interface Inheritance Statement, the PreparedStatement instance contains compiled SQL statements, so it executes faster than the Statement object.

2. As a subclass of Statement, PreparedStatement inherits all the functions of Statement. Three methods execute, ExecuteQuery, and executeupdate have been changed so that they no longer require parameters

3, in the JDBC application, if you are already a slight level developer, you should always replace with PreparedStatementStatement. In other words, do not use Statement at any time.
ImportJava.awt.Color;ImportJava.awt.Container;Importjava.awt.event.MouseEvent;ImportJava.awt.event.MouseListener;ImportJava.awt.event.MouseMotionListener;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;ImportJava.sql.ResultSetMetaData;Importjava.sql.SQLException;Importjava.sql.Statement;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;ImportJavax.swing.JPanel; Public classJavajdbcextendsJFrame {Connection Connection=NULL;//Driver Connection InterfaceStatement statement=NULL;//get an interface implementation objectResultSet rs=NULL;//result set interfaceResultSetMetaData rsmd=NULL;  PublicJavajdbc () {Try{class.forname ("Oracle.jdbc.driver.OracleDriver");//Load DriverString dburl= "Jdbc:oracle:thin: @localhost: 1521:ORCL";//connecting to an Oracle databaseString username= "admin";//login account and password for the databaseString password= "Admin"; Connection=drivermanager.getconnection (Dburl,username,password); Statement=connection.createstatement (); RS=statement.executequery ("SELECT * from student");//SQL query StatementsRsmd=Rs.getmetadata (); intLength=Rsmd.getcolumncount ();  for(inti=1;i<=length;i++) System.out.print (Rsmd.getcolumnname (i)+" ");            System.out.println ();  while(Rs.next ()) { for(inti=1;i<=length;i++) System.out.print (rs.getstring (i)+" ");            System.out.println (); }                    }Catch(ClassNotFoundException e) {//System.out.println ("Database driver loading failed ..." +e.getstacktrace ());E.printstacktrace (); }Catch(SQLException e) {System.out.println ("Database connection Failed ..." +e.getstacktrace ()); }finally{            Try{                if(rs!=NULL) {rs.close (); }                if(connection!=NULL) {connection.close (); }            }Catch(SQLException e) {e.getstacktrace (); }        }    }     Public Static voidMain (String args[]) {NewJavajdbc (); }}

PreparedStatement

ImportJava.awt.Color;ImportJava.awt.Container;Importjava.awt.event.MouseEvent;ImportJava.awt.event.MouseListener;ImportJava.awt.event.MouseMotionListener;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;ImportJava.sql.ResultSetMetaData;Importjava.sql.SQLException;Importjava.sql.Statement;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;ImportJavax.swing.JPanel; Public classJavajdbcextendsJFrame {Connection Connection=NULL;//Driver Connection InterfacePreparedStatement preparedstatement=NULL;//get an interface implementation objectResultSet rs=NULL;//result set interfaceResultSetMetaData rsmd=NULL;  PublicJavajdbc () {Try{class.forname ("Oracle.jdbc.driver.OracleDriver");//Load DriverString dburl= "Jdbc:oracle:thin: @localhost: 1521:ORCL";//connecting to an Oracle databaseString username= "admin";//login account and password for the databaseString password= "Admin"; Connection=drivermanager.getconnection (Dburl,username,password); String SQL= "SELECT * FROM Student"; //Create a PreparedStatement object to send the parameterized SQL statement to the database. Preparedstatement=connection.preparestatement (SQL); RS=preparedstatement.executequery ();//get the result setRsmd=Rs.getmetadata (); intLength=Rsmd.getcolumncount ();  for(inti=1;i<=length;i++) System.out.print (Rsmd.getcolumnname (i)+" ");            System.out.println ();  while(Rs.next ()) { for(inti=1;i<=length;i++) System.out.print (rs.getstring (i)+" ");            System.out.println (); }                    }Catch(ClassNotFoundException e) {//System.out.println ("Database driver loading failed ..." +e.getstacktrace ());E.printstacktrace (); }Catch(SQLException e) {System.out.println ("Database connection Failed ..." +e.getstacktrace ()); }finally{            Try{                if(rs!=NULL) {rs.close (); }                if(connection!=NULL) {connection.close (); }            }Catch(SQLException e) {e.getstacktrace (); }        }    }     Public Static voidMain (String args[]) {NewJavajdbc (); }}

Java-jdbc-oracle Database Connection

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.