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!

What’s the difference between Linux and Mac? Do you want to install Linux on Mac? This post from php.cn will show you all. You can refer to this guide to dual boot Linux and macOS.

What is MHTML? How to open or view it? What are the differences between it and HTML? How to convert MHTML to HTML? If you are looking for the answers to the above questions, you can refer to this post from php.cn.

This article focuses on the topic that deleted files keep reappearing in Windows 10, introducing the responsible reasons and feasible solutions.

This post teaches you how to deactivate Windows 10/11 by removing product key or license. You can use that product key to activate another computer later if you want. For more computer tips and tricks, you can visit php.cn Software official website.

Some Windows users report that they are prompted by the error 0164 memory size decreased screen every time they boot the computer. What’s wrong with it? If you are in the same boat, congratulations! You’ve come to the right place! In this post from p

This Page Isn’t Available Right Now is an error message you may encounter when you visit Facebook using your web browser. In this php.cn post, we will list some effective methods you can try to get rid of this error.

Windows 11 KB5010414, a new optional update for Windows 11, is available now. Do you know what’s new and fixes in it? php.cn Software will show you this information in this post. Besides, it also tells you how to download and install it on your compu

Want to pause Windows Update on your Windows 11 computer? Want to set active hours to arrange a computer restart to complete the update process? You need to know how to change Windows Update settings in Windows 11. This php.cn post will show you the


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use
