是的,在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("Connection established......");
//将值插入表
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: ");
//检索数据
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