How Java connects to Oracle database issues

Source: Internet
Author: User
Tags database issues administrator password

Oracle database is purely self-taught, no, please leave a message correct!

Before learning Oracle believe that you already know the MySQL or sqlsever database, this is not the same as the previous two, you install the time let you enter a password, it seems to be a system administrator password, and the Sqlsevser sa user similar, the permissions are very large, But it seems that no one in this database set up a database for that system user! Specifically do not know why!! Safety? (I installed here the first login with the account: System Password: You set the installation), I use Navicat premium Landing, see the online useful plsql .... After logging in, click on the user-user click on the Scott

Edit that account and then tick the locked account, then set a new password yourself!! Study (Student Dog!) Ah haha) General use of this account is enough

Then with the Scott account landing, she has a few tables below, do not care, you build a table directly built, the rest and MySQL no difference ... In addition to some other syntax ....

Here is the Java Link Database code

You set up a database yourself under Navicat press CTRL+Q to create a new query copy the following code execution

Reference Blog https://www.cnblogs.com/cainiaomahua/p/6183339.html

CREATE TABLE student (       ID number (one) not null primary key,       stu_name varchar (+) NOT NULL,       gender number (one) def Ault NULL, age number       (one) default NULL,       address varchar (+) default null);

Import jar package similar to MySQL ... You know

Create a project first, then right-click on the item to-->new-->folder;folder Name:lib, create a folder lib in the project, and then import the Ojdbc6.jar package into the folder

The package share address: Link: Https://pan.baidu.com/s/1jW_ofgU4eJmAn7Y2J5B46A Password: epkz

Create a Java class with the following code:

Package com. Util;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.resultset;import java.sql.SQLException; Public classDbutil { Public StaticConnection Getconn () {String driver="Oracle.jdbc.driver.OracleDriver";//This basically does not have to move fixedString URL ="JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL";//that 127.0.0.1 is your IP address, if you learn//with IP also do not change 1512 is the port number, ORCL is service_name this how to see? You open your Oracle installation directory, search Tnsnames.ora//use Notepad to open the search service_name equals sign is you want to fill!! String user ="Scott";//User nameString Password ="Root";//PasswordConnection conn =NULL; Try{class.forname (driver); System. out. println ("------Load Driver succeeded-----"); Conn=(Connection) drivermanager.getconnection (URL, user, password); System. out. println ("------The connection was successful-----"); } Catch(ClassNotFoundException e) {System. out. println ("------Load driver failed, driver class not found------");            E.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); System. out. println ("------Load driver failed------"); }         returnConn; }  Public StaticString Gedata () {Connection Connection=getconn ();Try{PreparedStatement PreparedStatement= Connection.preparestatement ("SELECT * FROM Student" ); ResultSet ResultSet=Preparedstatement.executequery ();  while(Resultset.next ()) {System. out. println (Resultset.getstring ("Stu_name")+"---name"); System. out. println (Resultset.getstring ("ADDRESS")+"---address"); System. out. println (Resultset.getint ("ID")+"---Id"); System. out. println (Resultset.getint (" Age")+"---Age"); System. out. println (Resultset.getint ("GENDER")+"---Little sister I slept with."); //connection.close ();            }    } Catch(SQLException e) {//TODO Auto-generated catch blockE.printstacktrace (); System. out. println ("Unexpected query error, please retry");} return NULL; }    Public Static voidInsert () {Connection Connection=Getconn (); Try{PreparedStatement PreparedStatement= Connection.preparestatement ("INSERT into student values (' 1723 ', ' Grass mud horse ', ' 171 ', ' 127 ', ' Paradise ')" );    Preparedstatement.executeupdate (); } Catch(SQLException e) {//TODO Auto-generated catch blockE.printstacktrace (); }              }      Public Static voidMain (string[] args) {insert (); Gedata ();}} 
View Code

Test:

How Java connects to Oracle database issues

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.