java—如何为jdbc fetch语句创建可测试代码

w6lpcovy  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(153)

我试图创建一个jdbc可测试的示例代码来学习。所以我从https://www.tutorialspoint.com/jdbc/jdbc-sample-code.htm 但是我很难创建可测试的方法。在eclipse中,我创建了一个默认的测试类,我正在考虑测试以检查结果是否返回7条记录(因为我正在从数据库的employee表中获取7条记录)
有人能给我建议一下吗?
我现在掌握的密码是

package src;

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

public class JdbcTestCode {

    public static void main(String[] args) {
        System.out.println("Start...");

        // JDBC driver name and database URL
        String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
        String DB_URL = "jdbc:oracle:thin:@..........";

        // Database credentials
        String USER = "please_login_here";
        String PASS = "please_login_here";

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            // STEP 2: Register JDBC driver
            Class.forName(JDBC_DRIVER);

            // STEP 3: Open a connection
            System.out.println("Connecting to database...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // STEP 4: Execute a query
            System.out.println("Creating statement...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT id, first_name, last_name FROM Employee ORDER BY id FETCH FIRST 7 ROWS ONLY";
            rs = stmt.executeQuery(sql);

            // STEP 5: Extract data from result set
            while (rs.next()) {
                // Retrieve by column name
                int id = rs.getInt("id");
                String first = rs.getString("first_name");
                String last = rs.getString("last_name");

                // Display values
                System.out.print("ID: " + id);
                System.out.print(", First: " + first);
                System.out.println(", Last: " + last);
            }

            // STEP 6: Clean-up environment

        } catch (SQLException se) {
            // Handle errors for JDBC
            se.printStackTrace();
        } catch (Exception e) {
            // Handle errors for Class.forName
            e.printStackTrace();
        } finally {
            // finally block used to close resources
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }

            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException se2) {
                se2.printStackTrace();
            } // nothing we can do
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se3) {
                se3.printStackTrace();
            } // end finally try
        } // end try
        System.out.println("testing withg JDBC driver");

        System.out.println("End...");

    }

}

eclipses创建的默认测试代码是package src;
导入静态org.junit.assert.*;

import org.junit.Test;

public class JdbcTestCodeTest {

    @Test
    public void test() {
        fail("Not yet implemented");
    }

}

我正处于学习阶段,所以如果我问一个非常简单的问题,请耐心听我说。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题