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

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:博客园. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.