Simple Java link to Oracle database

Source: Internet
Author: User
Tags uuid

Simply simulates the ability to transfer funds between bank accounts, which is divided into 4 packages in Java code

1. Call the stored procedure of the database

import java.sql.callablestatement;import java.sql.connection;import java.sql.preparedstatement; Import java.sql.resultset;import java.sql.sqlexception;import entity. Userinfo;import util. dbutil;public class dao {private static connection conn =  Dbutil.getconn ();//Link Database callablestatement cs = null;//precompiled Public string getconn ( Userinfo u) {string str = null;//defines a string that is used to receive the returned string//call to Oracle named zhuanzhang  stored procedure string  sql =  "{Call zhuangzhang (?,?,?,?)}"; Try {cs = conn.preparecall (SQL); Cs.setint (1, u.getuuid ()); Cs.setdouble (2, u.getMoney ( ); Cs.setint (3, u.getidd ()); Cs.registeroutparameter (4, java.sql.types.varchar); Cs.execute ();// The value obtained from the database is stored in STR and returned to str = cs.getstring (4);}  catch  (sqlexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();} Return str;}}

2, database links

import java.sql.connection;import java.sql.drivermanager;import java.sql.preparedstatement; import java.sql.resultset;import java.sql.sqlexception;/** *  class  *  @author of linked databases  Administrator * */public class DBUtil { private static  connection conn = null;//links   private static String url =    "Jdbc:oracle:"  +  "THIN:@127.0.0.1:1521:ORCL";// 127.0.0.1 is a native address, XE is the default database name for Lite Oracle  / /Using the Scott account, the password is set to root  which in the experiment gave Scott user dba Authority  private static String user =  " Scott "; private static string password = " root ";   public  Static connection getconn () {  try {class.forname ("Oracle.jdbc.driver.OracleDriver");}  catch  (classnotfoundexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}   Load orAcle driver   try {conn = drivermanager.getconnection (Url, user, password);}  catch  (sqlexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}   Get connection   return conn; }}

3. Encapsulated Entities

public class UserInfo {private int uuid;//idprivate double money;//the amount transferred out of the account private int IDD; idpublic int Getuuid () {return UUID) to receive the transfer; public void Setuuid (int uuid) {this.uuid = uuid;} Public double Getmoney () {return money;} public void Setmoney (double money) {This.money = money;} public int Getidd () {return IDD;} public void Setidd (int idd) {This.idd = IDD;}}

4, the code executes the entrance, here directly uses the Main method

Import Java.util.scanner;import DAO. Dao;import entity. Userinfo;public class Test {public static void main (string[] args) {Scanner sc = new Scanner (system.in); System.out.println ("Welcome to the self-service bank, please enter your account number:"); UserInfo u = new UserInfo ();D ao d = new Dao (); U.setuuid (Sc.nextint ()); System.out.println ("Please enter the amount to be transferred:"); U.setmoney (Sc.nextdouble ()); System.out.println ("Please enter the offset account:"), U.setidd (Sc.nextint ());//Put the object into the Getconn method inside the DAO, get the STR value returned by string str = D.getconn (u); System.out.println (str);}}


This article from "Strive For Excellence" blog, declined reprint!

Simple Java link to Oracle database

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.