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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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)

Discover How to Fix Drive Health Warning in Windows Settings Discover How to Fix Drive Health Warning in Windows Settings Mar 19, 2025 am 11:10 AM

What does the drive health warning in Windows Settings mean and what should you do when you receive the disk warning? Read this php.cn tutorial to get step-by-step instructions to cope with this situation.

How do I edit the Registry? (Warning: Use with caution!) How do I edit the Registry? (Warning: Use with caution!) Mar 21, 2025 pm 07:46 PM

Article discusses editing Windows Registry, precautions, backup methods, and potential issues from incorrect edits. Main issue: risks of system instability and data loss from improper changes.

How do I manage services in Windows? How do I manage services in Windows? Mar 21, 2025 pm 07:52 PM

Article discusses managing Windows services for system health, including starting, stopping, restarting services, and best practices for stability.

How do I change the default app for a file type? How do I change the default app for a file type? Mar 21, 2025 pm 07:48 PM

Article discusses changing default apps for file types on Windows, including reverting and bulk changes. Main issue: no built-in bulk change option.

How to Fix the Steam Cloud Error? Try These Methods How to Fix the Steam Cloud Error? Try These Methods Apr 04, 2025 am 01:51 AM

The Steam Cloud error can be caused by many reasons. To play a game smoothly, you need to take some measures to remove this error before you launch the game. php.cn Software introduces some best ways as well as more useful information in this post.

Windows Metadata and Internet Services Problem: How to Fix It? Windows Metadata and Internet Services Problem: How to Fix It? Apr 02, 2025 pm 03:57 PM

You may see the “A connection to the Windows Metadata and Internet Services (WMIS) could not be established.” error on Event Viewer. This post from php.cn introduces how to remove the Windows Metadata and Internet Services problem.

How to Resolve the KB5035942 Update Issues – Crashing System How to Resolve the KB5035942 Update Issues – Crashing System Apr 02, 2025 pm 04:16 PM

KB5035942 update issues - crashing system commonly happens to users. Inflicted people hope to find a way out of the kind of trouble, such as crashing system, installation, or sound issues. Targeting these situations, this post published by php.cn wil

How do I use the Group Policy Editor (gpedit.msc)? How do I use the Group Policy Editor (gpedit.msc)? Mar 21, 2025 pm 07:48 PM

The article explains how to use the Group Policy Editor (gpedit.msc) in Windows for managing system settings, highlighting common configurations and troubleshooting methods. It notes that gpedit.msc is unavailable in Windows Home editions, suggesting

See all articles