testnullvalues.java
来自「本套光盘提供了本书各章实例的所需的部分源程序文件以及数据库文件。读者 需要使用」· Java 代码 · 共 45 行
JAVA
45 行
import java.net.URL;
import java.sql.*;
public class TestNullValues {
public static void main(String[] args) {
String sql = "SELECT ID, Name, Description, Others "
+ "FROM FirstTable";
Connection connection = null;
Statement st = null;
ResultSet results = null;
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=JavaDB;Password=javadb;DatabaseName=northwind";
try {
Class.forName ("com.microsoft.jdbc.sqlserver.SQLServerDriver");
connection = DriverManager.getConnection (
url);
st = connection.createStatement();
results = st.executeQuery(sql);
String name, description, others;
int id;
while (results.next()) {
id = results.getInt("ID");
name = results.getString("Name");
description = results.getString("Description");
others = results.getString("Others");
if (results.wasNull()) {
others = "It's null";
}
System.out.println(id + ", " + name.trim() + ", "
+ description.trim()+ ", " + others.trim());
}
results.close();
st.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
finally {
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?