Oracle principle: JAVA connects to Oracle database (JDBC)

The first step is to create a new JAVA project and import the Oracle driver to the JAVA external dependency package:

The driver can also be found on the Internet. As long as you download Oracle, the Oracle driver exists, and the driver is the ojdbc6.jar file in the jdbc directory. , Mine is

D:\oracle\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar

Then copy it to the JAVA project, and then select the jar package Build Path -> Add to build path.

The first step is to connect to the database

The second step is to execute the SQL statement

The third part closes the database

Which connect and close the database can refer to:

package com.voapd;
import java.sql.*;
import java.util.ResourceBundle;

/**
 * jdbc工具类,负责:
 * 1. 加载/注册数据库驱动程序
 * 2. 获取数据库连接
 * 3. 释放数据库资源(Connection, Statement, ResultSet)
 */
public class TestJDBC {

    private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
    //URL = jdbc:oracle:thin:@    +   ip地址:端口   : orcl
    private static final String URL = "jdbc:oracle:thin:@192.168.0.101:1521:orcl";
    private static final String USERNAME = "voapd";
    private static final String PASSWORD = "voapd";

    static{
        try {
            //1. 注册数据库驱动程序
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            System.err.println("注册数据库驱动程序失败。" + e.getMessage());
        }
    }
    /**
     * 2. 获取数据库连接
     *
     * @return
     */
    public static Connection getConnection() {
        try {
            Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            return  conn;
        } catch (SQLException e) {
            System.err.println("a获得数据连接失败。" + e.getMessage());
        }
        return null;
    }

    /**
     * @param conn
     * @param stmt
     * @param rs
     */
    public static void close(Connection conn, Statement stmt, ResultSet rs) {
        try {
            //关闭数据库的资源的顺序最好与使用的顺序相反
            if(rs != null){
                rs.close();
            }
            if(stmt != null){
                stmt.close();
            }
            if(conn != null){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
   

    /**
     * 对数据库连接进行测试
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(getConnection());
    }
}

Execute SQL language can refer to

package com.voapd;
import java.sql.*;

public class TestSQL {
  private static Statement stmt=null;
  private static ResultSet rs=null;
  private static PreparedStatement ps=null;
  public static int DML(String state){
	  Connection conn= TestJDBC.getConnection();
	  int ret=-1;
	  try {
		ps=conn.prepareStatement(state);
		ret=ps.executeUpdate();
		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		TestJDBC.close(conn, stmt, rs);
	}
	  return ret;
  }
  public static ResultSet DQL(String state){
	  Connection conn= TestJDBC.getConnection();
	  ResultSet ret=null;
	  try {
		ps=conn.prepareStatement(state);
		stmt=conn.createStatement();
		rs=stmt.executeQuery(state);
		ret=rs;
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		TestJDBC.close(conn, stmt, rs);
	}
	  return ret;
  }
  public static void main(String[] args) {
	  System.out.println(DML("update salary_tbl set salary=123 where employer_nm ='雇佣者1' "));
	  System.out.println(DQL("select * from salary_tbl where rownum <5 "));
  }
  
}

Note that there is no need to add a semicolon in the string.

Guess you like

Origin blog.csdn.net/superSmart_Dong/article/details/104589877