Andy
Andy

Reputation: 195

How to determine database table column size in java

I use JDBC to update a SQL Server table in java. I update a nvarchar column. 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.

Is there a way to find out how long a text column is before I update it?

Thanks

Upvotes: 0

Views: 3309

Answers (2)

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

You can get the size using the sp_columns stored proc

e.g

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

There's a few others including querying the information_schema as well.

Upvotes: 2

maxx777
maxx777

Reputation: 1330

use getPrecision(int) method of ResultSetMetaData class.

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)

Upvotes: 2

Related Questions