Fourth day of training-----JDBC Connection to Oracle

Source: Internet
Author: User
Tags stmt

    • Oracle links the database and inserts a data into the Tableone
 Packagecom.zjw.db;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importjava.sql.Statement; Public classMydbbase {PrivateConnection Conn; PrivateString oracleurl= "Jdbc:oracle:thin: @localhost: 1521:orcl"; //private String mysqlurl= "Jdbc:mysql://localhost:3306//MySQL ";    PrivateString username = "U1"; PrivateString password = "Admin123";  Publicmydbbase () {Try{class.forname ("Oracle.jdbc.driver.OracleDriver"); Drivermanager.setlogintimeout (0); Conn=drivermanager.getconnection (Oracleurl,username,password); Statement stmt=conn.createstatement (); String SQL= "INSERT into Tableone (id,name,sex,age) VALUES (1, ' Jiewei ', ' Male ', ' 23 ')";            Stmt.executeupdate (SQL);            Conn.commit ();            Stmt.close ();        Conn.close (); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }     Public Static voidMain (string[] args) {mydbbase MB=Newmydbbase (); }}
    • Querying Tableone data in a database
 Packagecom.zjw.db;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement; Public classMydbbase {PrivateConnection Conn; PrivateString oracleurl= "Jdbc:oracle:thin: @localhost: 1521:orcl"; //private String mysqlurl= "Jdbc:mysql://localhost:3306//MySQL ";    PrivateString username = "U1"; PrivateString password = "Admin123";  Publicmydbbase () {Try{class.forname ("Oracle.jdbc.driver.OracleDriver"); Drivermanager.setlogintimeout (0); Conn=drivermanager.getconnection (Oracleurl,username,password); Statement stmt=conn.createstatement (); //INSERT Statement//String sql = "INSERT into Tableone (id,name,sex,age) VALUES (1, ' Jiewei ', ' Male ', ' 23 ')"; //String sql = "Delete from Tableone where id=1"; //stmt.executeupdate (SQL); //Query StatementsString sqlselect = "SELECT * FROM Tableone"; ResultSet RS=stmt.executequery (Sqlselect);  while(Rs.next ()) {System.out.print (Rs.getint ("id") + ""); System.out.print (Rs.getstring ("Name") + ""); System.out.print (Rs.getstring ("Sex") + ""); System.out.print (Rs.getstring ("Age") + "");            } conn.commit ();            Stmt.close ();        Conn.close (); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }     Public Static voidMain (string[] args) {mydbbase MB=Newmydbbase (); }}
  • Use the PreparedStatement to carry out the increase and deletion check
    //use the PreparedStatement to carry out the increase and deletion check     Public Static BooleanAuth (String name,string password) {conn=getconnection (); Try{PreparedStatement pstmt= Conn.preparestatement ("SELECT * from Tableone" + "where name=? and password =? "); Pstmt.setstring (1, name); Pstmt.setstring (2, password); ResultSet RS=Pstmt.executequery (); if(Rs.next ()) {return true; }        } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        return false; }

Fourth day of training-----JDBC Connection to Oracle

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.