JDBC create project and basic operation

Keywords: Java Oracle Maven

get ready

The Maven project is directly used here. The tool used is idea, the connection is Oracle (of course, the operation of mysql is almost the same). I hope you can do the code here, so as to ensure harvest.

Create Maven project

Create a new project, select Maven, select the jdk version, and do not use the skeleton

Enter the groupId and project name and create it.

Maven's warehouse and project code settings

Search maven in settings, change the warehouse location here to the local warehouse location you installed, check the User setting file below, and select the setting file in the corresponding directory.

Code changed to utf-8

Configuration dependency

Installing Oracle driver dependencies If you have installed it, you can skip it
Open the pom.xml configuration file and paste the following code

    <!--maven Used by the project jdk edition-->
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--introduce oracle Database dependency-->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.1.0</version>
        </dependency>
    </dependencies>

Note that the version numbers of jdk and oracle here must be the version installed locally. You can use cmd to view your version number

After that, you will be prompted to reload the changed Maven and click Convert

After that, there will be more jar packages provided by the database manufacturer in your project directory

Here, the preparatory work is finished, and the next step is to write code.

code


Create packages and your code in this java directory
I'll go straight to the code here.

Connect to Oracle Database

package com.symc.example0;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @Author: Fengwen Shenyang Medical College 2019 medical information engineering 0213
 * @CreateTime: 2021/09/23 11:00
 * @Description: Connect to Oracle Database
 */
public class Oracle_demo1 {
    public static void main(String[] args) {
        try {
            //1. Load the database driver through class loading
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //2. Get the database connection object through DriverManager
            //localhost and 127.0.0.1 both represent local addresses
            Connection connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@127.0.0.1:1521:ORCL",
                    "scott",
                    "tiger");
            System.out.println("oracle Database connection succeeded:" + connection);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}

This directory is the path where you load the driver. You can find it

scott and tiger are the accounts and passwords you want to connect, and the url is written in a fixed way.
After this code runs, if you encounter a problem, you need to solve it yourself. I hope the following articles can help you by chance. This is the problem and solution I have encountered
ORA-12505 If the exception you encounter is also this 12505, click this directly.

Addition, deletion and modification

Note: for the insert operation used here, you only need to change the following sql statements to delete and modify.

package com.symc.example0;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Author: Fengwen Shenyang Medical College 2019 medical information engineering 0213
 * @CreateTime: 2021/09/28 20:59
 * @Description: Statement,Oracle Basic operations insert data
 */
public class Oracle_Demo2 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:orcl",
                    "scott",
                    "tiger"
            );
            System.out.println("Oracle Database connection succeeded:"+ connection);
            //3. Create a Statement data operation object through the Connection object
            statement = connection.createStatement();
            //4. Create SQL statement
            String sql = "insert into dept values (50,'Medical letter','Shenyang')";
            //5. Insert here
            //i: Indicates the number of rows affected after insertion
            int i = statement.executeUpdate(sql);
            if (i>0) {
                System.out.println("Successfully inserted[" + i + "]Data!");
            }else{
                System.out.println("Insert failed!");
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            //Close the connection and free up resources
            //Closing principle: first open and then close, and then open and then close
            try {
                statement.close();
                connection.close();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }

            //There are no variables in the try block in the code block, so you should declare the Connection and Statement objects outside first
        }
    }
}

Query operation

The process here is no different from that above. I'm too lazy to write notes again.

package com.symc.example0;

import java.sql.*;

/**
 * @Author: Fengwen Shenyang Medical College 2019 medical information engineering 0213
 * @CreateTime: 2021/09/28 21:41
 * @Description: Connect to Oracle database and query basic operations
 */
public class Oracle_Demo3 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:orcl",
                    "scott",
                    "tiger");
            statement = connection.createStatement();
            System.out.println("Oracle Connection succeeded!");
            String sql = "select * from dept";
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                System.out.println(rs.getInt(1) + "\t" +
                        rs.getString("dname") + "\t" + rs.getString(3));
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                statement.close();
                connection.close();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }

        }
    }
}

Posted by basement on Sun, 03 Oct 2021 17:05:18 -0700