<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "upgrading UTF data from mysql 4.0 to 4.1 script"]]></title>
		<link>http://www.jforum.net/posts/list/5.page</link>
		<description><![CDATA[Latest messages posted in the topic "upgrading UTF data from mysql 4.0 to 4.1 script"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>upgrading UTF data from mysql 4.0 to 4.1 script</title>
				<description><![CDATA[ hello;<br /> during the migration process for 4.1 from 4.0 i have encountered several difficulties converting my UTF8 data in 4.0 (which does not support UTF-8 nativly) to 4.1 (which does support it)<br /> <br /> i have written a small (and already tested on my production jforum installation 2.0.2) program to help anyone to rapidly transform any UTF8 containing mysql 4.0 DB schema to a working 4.1 DB schema. the program basically runs over any DB table schema and as an output, the program writes a sequence of sql statements that change all character based columns (char, varchar, text) to UTF8 based column as described in mysql documentation here:<br /> [url]http://dev.mysql.com/doc/refman/4.1/en/charset-conversion.html[/url]<br /> <br /> all that is left to do by you is execute 'mysql "your db name"&lt;utf.sql'<br /> [quote]<br /> <br /> package test.utf;<br /> <br /> import java.io.File;<br /> import java.io.PrintWriter;<br /> import java.sql.Connection;<br /> import java.sql.DatabaseMetaData;<br /> import java.sql.DriverManager;<br /> import java.sql.ResultSet;<br /> import java.sql.Types;<br /> <br /> /**<br />  * @author Guy Katz<br />  */<br /> public class TestAlter {<br /> 	<br /> 	private static String dbName = "&lt;your DB name&gt;";<br /> 	private static String driverName = "com.mysql.jdbc.Driver";<br /> 	private static String username = "&lt;your username&gt;";<br /> 	private static String password = "&lt;your password&gt;";<br /> 	private static String url = "jdbc:mysql://localhost:3306/"+dbName+"?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf-8";<br /> 	<br /> 	public static void main(String[]args){<br /> 		//TODO get dbName, password, driverName,userName,URL from args<br /> 		try {<br /> 			Class.forName(driverName);<br /> 		} catch (ClassNotFoundException e) {<br /> 			e.printStackTrace();<br /> 		}<br /> 		new TestAlter().migrateUTF();<br /> 	}<br /> 	<br /> 	public TestAlter(){}<br /> 	<br /> 	public void migrateUTF(){<br /> 		try{<br /> 			File sqlCommandsOutputFile = new File("./utf.sql");<br /> 			PrintWriter write = new PrintWriter(sqlCommandsOutputFile);<br /> 			Connection con = DriverManager.getConnection(url, username, password);<br /> 			DatabaseMetaData dbmd = con.getMetaData();<br /> 			<br /> 			// Specify the type of object; in this case we want tables<br /> 			String[] types = {"TABLE"};<br /> 			ResultSet resultSet = dbmd.getTables(null, null, "%", types);<br /> 			<br /> 			// Get the table names<br /> 			while (resultSet.next()) {<br /> 				// Get the table name<br /> 				String tableName = resultSet.getString("TABLE_NAME");<br /> 				// Get the table's catalog and schema names (if any)<br /> 				System.out.println();<br /> 				System.out.println("TABLE="+tableName);<br /> 				<br /> 				ResultSet columnsResultSet = dbmd.getColumns(null,null,tableName,"%");<br /> 				while(columnsResultSet.next()){<br /> 					String colName = columnsResultSet.getString("COLUMN_NAME");<br /> 					String colType = columnsResultSet.getString("DATA_TYPE");<br /> 					String colSize = columnsResultSet.getString("COLUMN_SIZE");<br /> 					int colIntType = Integer.valueOf(colType).intValue();<br /> 					int colIntSize = Integer.valueOf(colSize).intValue();<br /> 					<br /> 					if(colIntType==java.sql.Types.CHAR || colIntType==java.sql.Types.VARCHAR || colIntType==java.sql.Types.LONGVARCHAR){<br /> 						System.out.println("COLUMN: name="+colName+" type="+colType+" size="+colSize);<br /> 						changeToUtf(write,tableName,colName,colIntSize,colIntType);<br /> 					}<br /> 					<br /> 				}<br /> 				System.out.println();<br /> 			}<br /> 			<br /> 			write.flush();<br /> 			write.close();<br /> 			con.close();<br /> 		}catch (Exception e) {<br /> 			e.printStackTrace();<br /> 		}<br /> 	}<br /> 	private void changeToUtf(PrintWriter write, String tableName, String colName, int colSize, int colIntType) {<br /> 		System.out.println("CHAGING TABLE="+tableName+" COLUMN="+colName);<br /> 		try{<br /> 			String alterSQL = "ALTER TABLE "+tableName+" MODIFY "+colName+" BINARY("+colSize+")";<br /> 			write.println(alterSQL+';');<br /> 			System.out.println("WRITING EXECUTE COMMAND="+alterSQL);<br /> 			String utfSQL ="ALTER TABLE "+tableName+" MODIFY "+colName+" "+mapColType(colIntType,colSize)+" CHARACTER SET utf8";<br /> 			write.println(utfSQL+';');<br /> 			System.out.println("WRITING EXECUTE COMMAND="+utfSQL);<br /> 		}catch (Exception e) {<br /> 			e.printStackTrace();<br /> 		}<br /> 	}<br /> <br /> 	private String mapColType(int colType, int colSize) {<br /> 		String result = null;<br /> 		if(colType==Types.CHAR){<br /> 			result = "CHAR("+Integer.toString(colSize * 1)+")";<br /> 		}else if(colType==Types.VARCHAR){<br /> 			result = "VARCHAR("+Integer.toString(colSize * 1)+")";<br /> 		}else if(colType==Types.LONGVARCHAR){<br /> 			result = "TEXT";<br /> 		}else{<br /> 			System.out.println("ERROR!! COULD NOT MAP SQL TYPE WITH VALUE="+colType);<br /> 		}<br /> 		return result;<br /> 	}<br /> }<br /> <br /> /*<br /> private String getDoubleColSize(int colType, int colSize) {<br /> String result = null;<br /> if(colType==Types.CHAR){<br /> result = Integer.toString(colSize*2);<br /> }else if(colType==Types.VARCHAR){<br /> result = Integer.toString(colSize*2);<br /> }else if(colType==Types.LONGVARCHAR){<br /> result = Integer.toString(colSize);<br /> }else{<br /> System.out.println("COULD NOT MAP SQL TYPE WITH VALUE="+colType);<br /> }<br /> return result;<br /> }<br /> */<br /> <br /> <br /> [/quote] <img src="http://www.jforum.net/images/smilies/8f7fb9dd46fb8ef86f81154a4feaada9.gif" border="0"> ]]></description>
				<guid isPermaLink="true">http://www.jforum.net/posts/preList/1750/7302.page</guid>
				<link>http://www.jforum.net/posts/preList/1750/7302.page</link>
				<pubDate><![CDATA[Mon, 30 Jan 2006 07:08:41]]> GMT</pubDate>
				<author><![CDATA[ gkatz]]></author>
			</item>
			<item>
				<title>upgrading UTF data from mysql 4.0 to 4.1 script</title>
				<description><![CDATA[ Great! I added it to Confluence:<br /> <br /> <a class="snap_shots" href="http://www.jforum.net/confluence/display/upgrade/MySQL+4.0+to+4.1+UTF8+converter" target="_blank" rel="nofollow">http://www.jforum.net/confluence/display/upgrade/MySQL+4.0+to+4.1+UTF8+converter</a><br /> <br /> Rafael]]></description>
				<guid isPermaLink="true">http://www.jforum.net/posts/preList/1750/7305.page</guid>
				<link>http://www.jforum.net/posts/preList/1750/7305.page</link>
				<pubDate><![CDATA[Mon, 30 Jan 2006 08:59:56]]> GMT</pubDate>
				<author><![CDATA[ Rafael Steil]]></author>
			</item>
			<item>
				<title>Re:upgrading UTF data from mysql 4.0 to 4.1 script</title>
				<description><![CDATA[ thanks;<br /> i just wanted to add 2 things to maybe make things clearer:<br /> <br /> 1. the program is non intrusive meaning that it will not modify anything in your DB, dont hesitate to use it and just view the output file and see if it is what you expected.<br /> <br /> 2. the program is generic, i used it once on my jforum database and the second time on my portal database (2 seperate mysql databases) and it worked fine.<br />  <img src="http://www.jforum.net/images/smilies/283a16da79f3aa23fe1025c96295f04f.gif" border="0"> ]]></description>
				<guid isPermaLink="true">http://www.jforum.net/posts/preList/1750/7307.page</guid>
				<link>http://www.jforum.net/posts/preList/1750/7307.page</link>
				<pubDate><![CDATA[Mon, 30 Jan 2006 09:17:00]]> GMT</pubDate>
				<author><![CDATA[ gkatz]]></author>
			</item>
			<item>
				<title>upgrading UTF data from mysql 4.0 to 4.1 script</title>
				<description><![CDATA[ Well, if you register for an account in Confluence, I can give you rights to change stuff there!<br /> <br /> Rafael]]></description>
				<guid isPermaLink="true">http://www.jforum.net/posts/preList/1750/7311.page</guid>
				<link>http://www.jforum.net/posts/preList/1750/7311.page</link>
				<pubDate><![CDATA[Mon, 30 Jan 2006 12:25:51]]> GMT</pubDate>
				<author><![CDATA[ Rafael Steil]]></author>
			</item>
	</channel>
</rss>