是否可以使用 JDBC 在表中的 VARCHAR2 列中存储和检索布尔值?
jdbcobject oriented programmingprogramming更新于 2025/6/30 16:07:17
是的,在 Oracle 中,您可以将布尔值存储和检索到 VARCHAR2 数据类型的表中。
如果这样做,则 true 和 false 值将分别存储为 1 和 0,检索结果也相同。
示例
让我们在 Oracle 数据库中创建一个名为 sampleTable 的表,并使用 CREATE 语句作为 −
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,在 IsBillDue 列中插入一个布尔值,该列的类型为 varchar2。
示例
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 {
//注册驱动程序
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//获取连接
String oracleUrl = "jdbc:oracle:thin:@localhost:1521/xe";
Connection con = DriverManager.getConnection(oracleUrl, "system", "password";);
System.out.println("连接已建立......");
//向表中插入值
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();
}
}
}
输出
连接已建立...... 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

