Java Operations Oracle

Source: Internet
Author: User

Tag:localhost   private    database connection    oracle   

public class dbcon {//  database Driven Object public static final string driver =   "Oracle.jdbc.driver.OracleDriver";//  database connection address (database name) public static final string  url =  "Jdbc:oracle:thin: @localhost: 1521:orcl";//  Landing name public static final string  USER =  "Scott";//  login password public static final string pwd =  " 123456 ";//  Creating a Database Connection object private connection con = null;//  Creating a Database precompiled Object private  preparedstatement ps = null;//  creating a result set private resultset rs = null;//   Create a data source object public static datasource source = null;public connection  Getcon ()  {try{class.forname (DRIVER); Con = drivermanager.getconnection (URL,&NBSP;USER,&NBSP;PWD) ; SYSTEM.OUT.PRINTLN ("Database connection succeeded! ");}  catch  (exception e)  {system.err.println ("Database connection failed! "); E.printstacktrace ();} Return cOn;} Public void closeall ()  {if  (rs != null) Try {rs.close ();}  catch  (sqlexception e)  {e.printstacktrace ();} if  (Ps != null) Try {ps.close ();}  catch  (sqlexception e)  {e.printstacktrace ();} if  (Con != null) Try {con.close ();}  catch  (sqlexception e)  {e.printstacktrace ();}} Public int update (String sql,string... pras) {           int resu=0;          con=getcon ();           try {               ps=con.preparestatement (SQL);               for (int i=0;i<pras.length;i++) {              &nBsp;    ps.setstring (I+1,pras[i]);               }               resu=ps.executeupdate ();          } catch  ( Sqlexception e)  {          }           finally{               closeall ();          }           return resu;      }   public resultset query (String sql,string... pras) {           con=getcon ();           try  {               ps=con.preparestatement (SQL);                 if (pras!=null)                    for (int i=0;i <pras.length;i++) {                       ps.setstring (I+1, pras[i]);                   }               rs=ps.executequery ();           } catch  (sqlexception e)  {               // todo auto-generated catch block      &Nbsp;        e.printstacktrace ();           }          return rs;       } public static void main (String[] args)  throws  sqlexception {dbcon dbcon = new dbcon ();//dbcon.getcon (); Resultset resultset = dbcon.query ("Select * from emp",  null);while  ( Resultset.next ())  {system.out.printf ("%-10d", Resultset.getint (1));         //output 10 columns, left-aligned (-sign left aligned)//system.out.print (Resultset.getint (1)  +  "\t\t"); System.out.printf ("%-10s", resultset.getstring (2));  //system.out.print (resultset.getstring (2)  +  "\t\t"); System.out.printf ("%-20s", Resultset.getstring (3));  //system.out.print (Resultset.getstring (3)  +  "\t\t\t\t"); System.out.printf ("%-10d", Resultset.getint (4);  //system.out.print (Resultset.getint (4)  +  "\t\t"); System.out.print (Resultset.getdate (5)  +  "\t\t"); System.out.print (resultset.getdouble (6)  +  "\t\t"); System.out.print (resultset.getdouble (7)  +  "\t\t"); System.out.println (Resultset.getint (8));} Dbcon.closeall ();}}

Jar Pack Download Http://yunpan.cn/QC5IeekKJvixN extract code 243b

This article is from "Avatar" blog, please make sure to keep this source http://shamrock.blog.51cto.com/2079212/1532489

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.