From 2d45af601f5f46ccf0acd048eb497f9392686c2c Mon Sep 17 00:00:00 2001 From: eric Date: Thu, 17 Feb 2005 19:44:45 +0000 Subject: [PATCH] - Added changeColumn, which allows you to change a column's definition. Can be used to change the name of a column as well (I.e., the definition is the same except you give it a new name.) --This line, and those below, will be ignored-- M src/java/com/samskivert/jdbc/JDBCUtil.java git-svn-id: https://samskivert.googlecode.com/svn/trunk@1591 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/jdbc/JDBCUtil.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/projects/samskivert/src/java/com/samskivert/jdbc/JDBCUtil.java b/projects/samskivert/src/java/com/samskivert/jdbc/JDBCUtil.java index 4c0a2475..fa462682 100644 --- a/projects/samskivert/src/java/com/samskivert/jdbc/JDBCUtil.java +++ b/projects/samskivert/src/java/com/samskivert/jdbc/JDBCUtil.java @@ -323,6 +323,34 @@ public class JDBCUtil "'."); } + /** + * Changes a column's definition. Takes a full column definition + * 'cdef' (including the name of the column) with which to replace the + * specified column 'cname'. + * + * NOTE: A handy thing you can do with this is to rename a column by + * providing a column definition that has a different name, but the + * same column type. + */ + public static void changeColumn (Connection conn, String table, + String cname, String cdef) + throws SQLException + { + String update = "ALTER TABLE " + table + " CHANGE " + cname + " " + + cdef; + + PreparedStatement stmt = null; + try { + stmt = conn.prepareStatement(update); + stmt.executeUpdate(); + } finally { + close(stmt); + } + + Log.info("Database column '" + cname + "' of table '" + table + + "' modified to have this def '" + cdef + "'."); + } + /** * Removes a named index from the specified table. */