Home Computer Tutorials Computer Knowledge Oracle Example of Java Driver Implementing Native Protocol

Oracle Example of Java Driver Implementing Native Protocol

Jan 15, 2024 pm 04:15 PM

Pure java driver Oracle instance about local protocol

package util;

import java.sql.*;

public class JdbcUtil {

static {

String driver = "oracle.jdbc.driver.OracleDriver"; //Load the driver, only need to load it once

try {

Class.forName(driver);

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

//Static method to obtain connection

public static Connection getConnection() { In programming, we often need to interact with databases. In this process, obtaining a database connection is a very critical step. In Java, we can use JDBC to implement database connections. The getConnection() method is a static method in JDBC, used to obtain the database connection object. This method usually requires passing in some parameters, such as the URL of the database, user name and password, etc. The specific parameters will vary depending on the database you are using. When writing code, you can follow the steps below to use the getConnection() method to get

String url = "jdbc:oracle:thin:@127.0.0.1:1521:database";

String user = "username";

String pwd = "password";

Connection con = null;

try {

con = DriverManager.getConnection(url, user, password);

} catch (SQLException e) {

e.printStackTrace();

}

return con;

}

}

The source code of the urgent java graduation project topic selection system with the database connection is

Core code:

public void actionPerformed(ActionEvent e) { This method is used to handle triggered events. In this method, you can write appropriate code to respond to the event. Different actions can be performed based on the type of event, such as performing certain actions when a button is clicked, or performing other actions when a menu item is selected. In this method, you can use the event object e to obtain relevant information, such as obtaining the component that triggered the event, obtaining the event type, etc. According to specific needs, we can write corresponding logic code in this method to achieve what we want

if(e.getSource() == add){

this.setVisible(false);

new AddPanel();

}

if(e.getSource() == modify){

this.setVisible(false);

new ModifyPanel();

}

if(e.getSource() == search){

this.setVisible(false);

new SearchPanel();

}

if(e.getSource() == exit){

System.exit(0);

}

}

..........

With complete source code

Use java to get data from Oracle database

8.Oracle8/8i/9i database (thin mode)

//import java.sql.*;

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); In this line of code, we use Java's reflection mechanism to dynamically load and instantiate the Oracle database driver. By calling the forName method of the Class class and passing in the driver's fully qualified name "oracle.jdbc.driver.OracleDriver" as a parameter, we can load the driver into memory. Next, use the newInstance method to create an instance object of the driver. In this way, we can use the driver to connect and operate the Oracle database in subsequent code.

String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl is the SID of the database, indicating that the Oracle database instance name connected to the local host is orcl

Connection conn = DriverManager.getConnection(url, "username", "password");

Statement stmtNew = conn.createStatement(); creates a Statement object associated with the database connection. This Statement object can be used to execute SQL statements and return results.

20. Store binary field data in the database

InputStream pic = new FileInputStream(dto.get(i).getLibPic()); is a line of code about the input stream. Its function is to create an input stream pic based on the libPic path specified by the i-th element of dto. libPic is a file path. This file is converted into an input stream through FileInputStream for subsequent operations.

sql = "INSERT INTO piclib (name,pic,sign,remark) VALUES (?,?,?,?)";

pstmt = con.prepareStatement(sql);

pstmt.setString(1, dto.get(i).getName()); This line of code sets the name of the i-th object in the list as the first parameter of PreparedStatement.

pstmt.setBinaryStream(2, pic, (int)dto.get(i).getLibPic().length()); The pic in the statement is a binary stream used to store picture data. Here, we pass pic as the second parameter to the setBinaryStream method of the pstmt object. And dto.get(i).getLibPic().length() is the length of the obtained image data, which is converted to int type and passed to the setBinaryStream method as the third parameter. In this way, the image data can be stored in the database.

21. Retrieve binary field data from the database

//import java.sql.*;

public class DemoDisplayBinaryDataFromDatabase {

public static Connection getConnection() throws Exception {

String driver = "oracle.jdbc.driver.OracleDriver";

String url = "jdbc:oracle:thin:@localhost:1521:databaseName";###

String username = "name";

String password = "password";

Class.forName(driver);

Connection conn = DriverManager.getConnection(url, username, password); is the code used to establish a database connection in Java. It uses the JDBC (Java Database Connectivity) API to obtain a connection to the database by specifying the URL, username and password of the database. This line of code is a key step in connecting to the database. It will return a Connection object. We can use this object to perform subsequent database operations, such as executing SQL statements, querying the database, etc.

return conn;

}

public static void main(String args[]) throws Exception { //Write your code logic here }

Connection conn = null;

ResultSet rs = null;

PreparedStatement pstmt = null;

String query = "SELECT raw_column, long_raw_column FROM binary_table WHERE id = ?";

try {

conn = getConnection();

Object[] results = new Object[2];

pstmt = conn.prepareStatement(query); is a common line of Java code used to create a precompiled SQL statement object. This object can be used to perform database queries or update operations. By passing a specific SQL query or update statement to this method, we can prepare a query or update operation that can be used repeatedly. This can improve the efficiency of database operations and prevent SQL injection attacks. This method requires a valid database connection object conn and a SQL statement query as parameters. By calling this method, we can get

pstmt.setString(1, "0001");

rs = pstmt.executeQuery();

rs.next();

Implement binary data on the client

results[0] = rs.getBytes("RAW_COLUMN");

results[1] = rs.getBytes("LONG_RAW_COLUMN");

} finally {

rs.close();

pstmt.close();

conn.close();

}

}

}

The above is the detailed content of Oracle Example of Java Driver Implementing Native Protocol. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Fixdisk Windows 7: Check Your Hard Disk for Errors on Windows 7 Fixdisk Windows 7: Check Your Hard Disk for Errors on Windows 7 Apr 14, 2025 am 12:40 AM

If you suspect your hard drive encounters issues, you can check the drive for errors on Windows 7. This php.cn post talks about fixdisk Windows 7. You can follow the guide to check the hard drive for errors on Windows 7.

Is Core Isolation Blocked by ew_usbccgpfilter.sys? Here Are Fixes! Is Core Isolation Blocked by ew_usbccgpfilter.sys? Here Are Fixes! Apr 13, 2025 am 12:47 AM

Many SurfaceBook users report that they meet the “core isolation blocked by ew_usbccgpfilter.sys” issue on Windows 11/10. This post from php.cn helps to fix the annoying issue. Keep on your reading.

Effortles Fixes for Black Screen After Installing a Graphics Driver Effortles Fixes for Black Screen After Installing a Graphics Driver Apr 15, 2025 am 12:11 AM

Have you ever encountered a black screen after installing a graphics driver like an Nvidia driver in Windows 10/11? Now in this post from php.cn, you can find a couple of worth trying solutions to the Nvidia driver update black screen.

KB2267602 Fails to Install: Here Is How to Fix It! KB2267602 Fails to Install: Here Is How to Fix It! Apr 15, 2025 am 12:48 AM

KB2267602 is a protection or definition update for Windows Defender designed to fix vulnerabilities and threats in Windows. Some users reported that they were unable to install KB2267602. This post from php.cn introduces how to fix the “KB2267602 fai

Difference Between RAID Recovery and Hard Drive Recovery Difference Between RAID Recovery and Hard Drive Recovery Apr 17, 2025 am 12:50 AM

Data recovery is always a heated topic. To successfully restore data from your device, you should know how it stores data. You can learn the difference between RAID recovery and hard drive recovery from this php.cn post.

How to Fix the File System Error (-1073741521) in Windows? - MiniTool How to Fix the File System Error (-1073741521) in Windows? - MiniTool Apr 16, 2025 am 12:37 AM

File system errors commonly happen on people’s computer and the error can trigger a series of linked malfunctions. This article on php.cn Website will give you a series of fixes to targeting the file system error (-1073741521). Please keep on with yo

How to Fix FileType Selected Not Supported by This App How to Fix FileType Selected Not Supported by This App Apr 13, 2025 am 12:41 AM

Are you suffering from the error message "FileType selected not supported by this app" when opening files in Teams or Excel? Now read this post from php.cn to get several useful solutions to this issue.

Fix Security Tab Not showing in Folder Properties Windows 11 Fix Security Tab Not showing in Folder Properties Windows 11 Apr 17, 2025 am 12:36 AM

The Security tab in File Properties helps set different permissions for different groups and users to a file or folder. Some users find that Windows 11 Security tab missing from File Properties. This post from php.cn gives some methods to fix it.

See all articles