Home  >  Article  >  Java  >  Introduction to the method of connecting Oracle database with JDBC in java (with code)

Introduction to the method of connecting Oracle database with JDBC in java (with code)

不言
不言forward
2019-02-22 13:17:395598browse

The content of this article is an introduction to the method of connecting Oracle database through JDBC in Java (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Load the oracle driver jar package, ojdbc6.jar

2. Build path *.jar

BuildPath only supports adding jar files. The specific method is as follows: In eclips Right-click on the project name->build path->contigure bud path->java build path There are libraries->

add external jars = add packages external to the project;

add jars = add project package;

add library = add a library;

add class folder = add a class folder

Introduction to User Libraries in add library Add User Library, the specific method is as follows:

1. Right-click the project->build path->Add Libraries...

2. Select User Library->next

3. Click the User Library button

4. Click the new button

5. Enter Library name

6. Click ok

7. Select The user library, and then click add jars

8. Find the corresponding jar package and confirm in sequence.

3. Connect to the database

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.ResultSet;
//import java.sql.Statement;
import java.sql.PreparedStatement;
public class Jdbcconnect {   public static void main(String[] args) {   
final String URL = "jdbc:oracle:thin:@ip/实例名";  
final String USER = "数据库用户名";   
final String PWD = "密码";
  try {  
  //加载驱动程序    Class.forName("oracle.jdbc.OracleDriver");   
 System.out.println("加载驱动成功!");  
 }catch (Exception  e){    System.out.println("加载驱动异常"+e);  
 }   try {    //获得数据库连接   
 Connection con = DriverManager.getConnection(URL,USER,PWD);    
System.out.println("加载数据库连接成功!");
   String sql = "select * from t_sys_user";
   PreparedStatement pt  = con.prepareStatement(sql);    
ResultSet rs = pt.executeQuery();   
 System.out.println(rs.next());   
 while(rs.next()) {    
 String userid = rs.getString("userid");    
 System.out.println(userid);    }   
 rs.close();    pt.close();    
con.close();
      }catch (SQLException e){    System.out.println("加载数据库连接异常"+e);   }     }  
}

The above is the detailed content of Introduction to the method of connecting Oracle database with JDBC in java (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete