简体   繁体   English

如何在Java中确定数据库表的列大小

[英]How to determine database table column size in java

I use JDBC to update a SQL Server table in java. 我使用JDBC更新Java中的SQL Server表。 I update a nvarchar column. 我更新了nvarchar列。 The thing is I don't know how long this column is. 问题是我不知道此专栏有多长时间。 If it has only 30 characters and I update it with 35 characters then it will fail. 如果它只有30个字符,而我将其更新为35个字符,则它将失败。

Is there a way to find out how long a text column is before I update it? 有没有一种方法可以在我更新文本列之前找出它的长度?

Thanks 谢谢

use getPrecision(int) method of ResultSetMetaData class. 使用ResultSetMetaData类的getPrecision(int)方法。

ResultSet rs;

// more code

ResultSetMetaData rsmd = rs.getMetaData();
int size=rsmd.getPrecision(COLUMN_INDEX);

http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSetMetaData.html#getPrecision(int) http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSetMetaData.html#getPrecision(int)

You can get the size using the sp_columns stored proc 您可以使用sp_columns存储的proc获取大小

eg 例如

exec sp_columns @table_name =  'MyTable', @column_name = 'MyColumn'

There's a few others including querying the information_schema as well. 还有其他一些,包括查询information_schema。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM