是否可以在表中使用 JDBC 將布林值儲存在 VARCHAR2 列中並從中檢索資料?


是的,在 Oracle 中,您可以將布林值儲存在表中,該表的列資料型別為 VARCHAR2。

如果您這樣做,則 true 和 false 值將分別儲存為 1 和 0,並作為相同值(分別)檢索。

示例

讓我們使用CREATE 語句在 Oracle 資料庫中建立一個名為**sampleTable** 的表,如下所示:

CREATE TABLE sampleTable(
   ID INT,
   ProductName VARCHAR (20) NOT NULL,
   CustomerName VARCHAR (20) NOT NULL,
   IsBillDue VARCHAR (20) NOT NULL,
   DeliveryDate date,
   Price INT,
   Location varchar(20)
);

該列IsBillDue 指定是否已支付賬單。

下面的 JDBC 程式建立與 Oracle 資料庫的連線,並填充 sampleTable 表,在型別為 varchar2 的 IsBillDue 列中插入一個布林值。

示例

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class BooleanTest {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
      //Getting the connection
      String oracleUrl = "jdbc:oracle:thin:@localhost:1521/xe";
      Connection con = DriverManager.getConnection(oracleUrl, "system", "password");
      System.out.println("Connection established......");
      //Inserting values to a table
      String query = "INSERT INTO sampleTable values (?, ?, ?, ?, ?, ?) ";
      PreparedStatement pstmt = con.prepareStatement(query);
      pstmt.setString(1, "Key-Board");
      pstmt.setString(2, "Raja");
      pstmt.setBoolean(3, true);
      pstmt.setDate(4, new Date(1567296000000L));
      pstmt.setInt(5, 7000);
      pstmt.setString(6, "Hyderabad");
      pstmt.execute();
      pstmt.setString(1, "Earphones");
      pstmt.setString(2, "Roja");
      pstmt.setBoolean(3, false);
      pstmt.setDate(4, new Date(1556668800000L));
      pstmt.setInt(5, 2000);
      pstmt.setString(6, "Vishakhapatnam");
      pstmt.execute();
      pstmt.setString(1, "Mouse");
      pstmt.setString(2, "Puja");
      pstmt.setBoolean(3, true);
      pstmt.setDate(4, new Date(1551398399000L));
      pstmt.setInt(5, 3000);
      pstmt.setString(6, "Vijayawada");
      pstmt.execute();
      pstmt.setString(1, "Mobile");
      pstmt.setString(2, "Vanaja");
      pstmt.setBoolean(3, false);
      pstmt.setDate(4, new Date(1551395452000L));
      pstmt.setInt(5, 9000);
      pstmt.setString(6, "Chennai");
      pstmt.execute();
      System.out.println("Contents of the table: ");
      //Retrieving data
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from sampleTable");
      while(rs.next()) {
         System.out.print("Name: "+rs.getString("ProductName")+", ");
         System.out.print("Customer Name: "+rs.getString("CustomerName")+", ");
         System.out.print("Dispatch Date: "+rs.getString("ISBILLDUE")+", ");
         System.out.print("Delivery Time: "+rs.getTime("DELIVERYDATE")+", ");
         System.out.print("Price: "+rs.getInt("PRICE")+", ");
         System.out.print("Location: "+rs.getString("LOCATION"));
         System.out.println();
      }
   }
}

輸出

Connection established......
Contents of the table:
Name: Key-Board, Customer Name: Raja, Dispatch Date: 1, Delivery Time: 00:00:00, Price: 7000, Location: Hyderabad
Name: Earphones, Customer Name: Roja, Dispatch Date: 0, Delivery Time: 00:00:00, Price: 2000, Location: Vishakhapatnam
Name: Mouse, Customer Name: Puja, Dispatch Date: 1, Delivery Time: 00:00:00, Price: 3000, Location: Vijayawada
Name: Mobile, Customer Name: Vanaja, Dispatch Date: 0, Delivery Time: 00:00:00, Price: 9000, Location: Chennai

更新日期:2019 年 7 月 30 日

505 次瀏覽

開啟你的 職業 生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.