Java Connection to Oracle database

Source: Internet
Author: User

First, you import the Ojdbc6.jar package (placed in the Lib folder)

Then you need to build a student table in the database to test:

Connection and test code:

ImportJava.security.interfaces.RSAKey;ImportOracle.jdbc.*;ImportJava.sql.*; Public classtestoracle {/**     * @paramargs*/     Public Static voidMain (string[] args) {//Load Driver        Try{class.forname ("Oracle.jdbc.OracleDriver"); //get the following URL: Window--open perspective-->myeclipse DataBase browser--new--driver templete Select thin,//Username,pass,jar Package And so on all write, import. Then test it and if the prompt is successful, you can copy that URL .//url:jdbc:oracle:thin:@<server>[:<1521>]:<database_name>String url= "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL";//Database Connection AddressString user= "root";//Database user nameString pass= "111";//Password//Obtaining an Oracle database connection through the drive managerConnection conn=drivermanager.getconnection (Url,user,pass); //Add a recordStatement st=conn.createstatement (); //Add Statement//String sql1= "insert into student values (1, ' aaa ', ' 123 ')";String sql2= "INSERT into student values (2, ' BBB ', ' 20170107 ')"; //Modifying statementsString sql3= "Update student set pass= ' 99912 ' where sid=1"; //DELETE statement//String sql= "Delete from sudent where sid=1"; //EXECUTE Statement//St.execute (SQL1); //St.execute (SQL3); //Query Statement (read all records)//String sql= "SELECT * FROM Student order by Sid"; //String sql= "select * from student where name= ' aaa ' and pass= ' 111 ' ORDER by Sid"; //Fuzzy Query//String sql= "select * from student where name is like ' a% '";//' a% ': Start with a. ' _a% '//ResultSet rs = st.executequery (SQL); //traverse the result output record://determine if the next record exists, and if so, the cursor moves backward one cell//While (Rs.next ()) {//System.out.println (rs.getstring ("Sid") + ";" +rs.getstring ("name") + ";" +rs.getstring ("pass"));//                //System.out.println (rs.getstring ("num")); //            }                        //precompilation: Record insertionString sql= "INSERT into student values (?,?,?)";//? Make a placeholder and post-precompile. PreparedStatement ps=conn.preparestatement (SQL); //Input ParametersPs.setint (1, 6);//The first one is the parameter index, and the second is the value. Ps.setstring (2, "SSS"); Ps.setstring (3, "18"); if(Ps.execute ()) {System.out.println ("Record added successfully"); }                        //precompilation: Modifying Records//String sql= "update student set pass=? where Name=?";//preparedstatement ps=conn.preparestatement (SQL);//ps.setstring (1, "666");//ps.setstring (2, "AAA");//        //Ps.execute ();//                                            //precompilation: Deleting Records//String sql= "Delete from student where sid=?";//preparedstatement ps=conn.preparestatement (SQL);//Ps.setin (1, 1);//            //Ps.execute (); //            //precompilation: Finding//String sql= "select * from student where name is like?" "; //This can only be a question mark, the question mark cannot be placed inside the string//preparedstatement ps=conn.preparestatement (SQL);//ps.setstring (1, "%a%");//fuzzy query conditions are given here////rs=ps.executequery ();//        //            //Output Results//While (Rs.next ()) {//System.out.println (rs.getstring ("Sid") + ";" +rs.getstring ("name") + ";" +rs.getstring ("pass"));//            }                        //System.out.println ("database connection succeeded");System.out.println ("Record added successfully"); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }}

To refresh data for a database table:

Java Connection 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.