48 lines
2.8 KiB
Java
48 lines
2.8 KiB
Java
import java.io.FileInputStream;
|
|
import java.sql.*;
|
|
import java.util.Properties;
|
|
public class BUserStruct {
|
|
public static void main(String[] args) throws Exception {
|
|
Properties props = new Properties();
|
|
try (FileInputStream in = new FileInputStream(args[0])) { props.load(in); }
|
|
try (Connection conn = DriverManager.getConnection(props.getProperty("url"), props.getProperty("username"), props.getProperty("password"))) {
|
|
System.out.println("== b_user column definitions ==");
|
|
String sql =
|
|
"SELECT c.name AS col, t.name AS type, c.max_length, c.precision, c.scale, c.is_nullable, " +
|
|
" c.is_identity, dc.definition AS default_def, c.column_id " +
|
|
"FROM sys.columns c " +
|
|
"JOIN sys.types t ON t.user_type_id = c.user_type_id " +
|
|
"LEFT JOIN sys.default_constraints dc ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id " +
|
|
"WHERE c.object_id = OBJECT_ID('b_user') ORDER BY c.column_id";
|
|
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql)) {
|
|
while (rs.next()) {
|
|
String type = rs.getString("type");
|
|
int maxLen = rs.getInt("max_length");
|
|
String typeSql;
|
|
if (type.equals("nvarchar") || type.equals("nchar") || type.equals("varchar") || type.equals("char")) {
|
|
typeSql = type + (maxLen == -1 ? "(max)" : "(" + (maxLen / (type.startsWith("n") ? 2 : 1)) + ")");
|
|
} else if (type.equals("decimal") || type.equals("numeric")) {
|
|
typeSql = type + "(" + rs.getInt("precision") + "," + rs.getInt("scale") + ")";
|
|
} else { typeSql = type; }
|
|
System.out.printf(" col=%-22s %-16s null=%s identity=%s default=%s%n",
|
|
rs.getString("col"), typeSql, rs.getBoolean("is_nullable"),
|
|
rs.getBoolean("is_identity"), rs.getString("default_def"));
|
|
}
|
|
}
|
|
System.out.println("== b_user 001/002 data ==");
|
|
try (Statement st = conn.createStatement();
|
|
ResultSet rs = st.executeQuery("SELECT * FROM b_user WHERE b_account IN ('001','002')")) {
|
|
ResultSetMetaData meta = rs.getMetaData();
|
|
while (rs.next()) {
|
|
StringBuilder sb = new StringBuilder();
|
|
for (int i = 1; i <= meta.getColumnCount(); i++) {
|
|
if (i > 1) sb.append(", ");
|
|
sb.append(meta.getColumnName(i)).append("=").append(rs.getObject(i));
|
|
}
|
|
System.out.println(" " + sb);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|