如何在java中使用不同的列方法?(How to use different column methods in java?)
优质
小牛编辑
127浏览
2023-12-01
问题描述 (Problem Description)
如何使用不同的列方法来计算列数,获取列名,获取列类型等?
解决方案 (Solution)
以下示例使用getColumnCount,getColumnName,getColumnTypeName,getColumnDisplaySize方法来获取列的列数,列的名称或表中列的类型。
import java.sql.*;
public class jdbcConn {
public static void main(String[] args) throws Exception {
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con = DriverManager.getConnection(
"jdbc:derby://localhost:1527/testDb","name","pass");
Statement stmt = con.createStatement();
String query = "select * from emp order by name";
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println("no of columns in the table = "+ rsmd.getColumnCount());
System.out.println("Name of the first column "+ rsmd.getColumnName(1));
System.out.println("Type of the second column "+ rsmd.getColumnTypeName(2));
System.out.println("No of characters in 3rd column "+ rsmd.getColumnDisplaySize(2));
}
}
结果 (Result)
上面的代码示例将产生以下结果。 结果可能会有所不同。
no of columns in the table= 3
Name of the first columnID
Type of the second columnVARCHAR
No of characters in 3rd column20