Object-Oriented Programming (Java) Experiment 12

Experiment content and experiment purpose

1. Purpose of the experiment

  1. Master the basic methods of Java database programming.
    2. Experimental content
    Implement the following programs on the computer and observe the running conditions of the programs:
  2. Create a student database School, create a table studb, including fields: student number, name, gender, major, year of admission. Write a program to add, delete, modify, and check student information on the studb table.

experiment procedure

In the experimental requirements, there is no requirement whether to connect to the database remotely or locally. Here, a remote connection is used (the database experiment has just learned jdbc, so it is directly applied here)

Remote Connection

The remote connection is based on the OpenGauss database and requires postgresql.jarpackages.

For environment configuration, please refer to:
Database Experiment 9

Simply put, the environment configuration steps

  1. Modify postgresql.conf
  2. Modify pg_hba.conf
  3. Create a new user (openGauss cannot use the default account omm to connect)
  4. Assign permissions to the newly created account
  5. (Configuring client access authentication seems to need to look at other configurations of the computer environment, some do not require this step)

Code:
Creating tables remotely requires granting role system permissions

GRANT ALL PRIVILEGES TO shiyanjiu;

(ps: The user called shiyanjiubecause the database experiment nine is a remote connection)

insert image description here

package opengauss;

import java.sql.*;

public class java {
    
    

        //以下代码将获取数据库连接操作封装为一个接口,可通过给定用户名和密码来连接数据库。
        public static void main(String[] args) throws SQLException
        {
    
    
                //驱动类。
                String driver = "org.postgresql.Driver";
                //数据库连接描述符。
                String sourceURL = "jdbc:postgresql://192.168.56.124:26000/db_cc";
                Connection conn = null;

                try
                {
    
    
                        //加载驱动。
                        Class.forName(driver);
                }
                catch( Exception e )
                {
    
    
                        e.printStackTrace();
                }

                try
                {
    
    
                        //创建连接。
                        conn = DriverManager.getConnection(sourceURL, "shiyanjiu","openGauss@123");

                        Statement stmt = null;
                        // 执行查询
                        stmt = conn.createStatement();
                        //插入
                        String sql = "create table studb" +//建表的SQL语句
                                "(  sno int primary key," +
                                "   sname char(20)," +
                                "   sex char(5)," +
                                "   speciality char(20)," +
                                "   date date" +
                                " );";
                        Statement statement = conn.createStatement();
                        int rows = statement.executeUpdate(sql);

                        System.out.println("数据表创建成功");

                        statement.close();//关闭
                        conn.close();

                }
                catch(Exception e)
                {
    
    
                        e.printStackTrace();
                        return ;
                }

                return;
        }


}

insert image description here
sqlIf you add, delete or modify, just change the statement in the code.

package opengauss;

import java.sql.*;

public class java {
    
    

        //以下代码将获取数据库连接操作封装为一个接口,可通过给定用户名和密码来连接数据库。
        public static void main(String[] args) throws SQLException
        {
    
    
                //驱动类。
                String driver = "org.postgresql.Driver";
                //数据库连接描述符。
                String sourceURL = "jdbc:postgresql://192.168.56.124:26000/db_cc";
                Connection conn = null;

                try
                {
    
    
                        //加载驱动。
                        Class.forName(driver);
                }
                catch( Exception e )
                {
    
    
                        e.printStackTrace();
                }

                try
                {
    
    
                        //创建连接。
                        conn = DriverManager.getConnection(sourceURL, "shiyanjiu","openGauss@123");

                        Statement stmt = null;
                        // 执行查询
                        stmt = conn.createStatement();
                        //插入
                        String sql = "insert into studb values('0001','张三','男','计算机类','2020-9-10');";
                        Statement statement = conn.createStatement();
                        int rows = statement.executeUpdate(sql);

                        System.out.println("数据插入成功");

                        statement.close();//关闭
                        conn.close();

                }
                catch(Exception e)
                {
    
    
                        e.printStackTrace();
                        return ;
                }

                return;
        }


}

insert image description here
Connection database check:
insert image description hereinsert image description here

Guess you like

Origin blog.csdn.net/qq_51594676/article/details/124998786