Home >Computer Tutorials >Computer Knowledge >Oracle Example of Java Driver Implementing Native 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;
}
}
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
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!