| Author |
How to insert the multi lang database into MySQl
|
Keerthi Kumar
Ranch Hand
Joined: Apr 20, 2009
Posts: 105
|
|
Hi,
I am working on one asset called Multi Language database asset. Here I need to insert some values from a properties file into MySQL. Since the data in the properties file is having different languages as well, when it is inserted, those different languages are getting inserted as question marks (???). Please do the needful to resolve this issue. Please find my java code below and let me know what changes needs to be made on the same.
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;
public class InsertTranslation
{
private static Connection DBconnection = null;
private static String server;
private static String port;
private static String dbase;
private static String uname;
private static String pword;
private static final String BUNDLE = "InsertTranslation.properties";
public static void main(String args[])
{
try
{
String environment = "PROD"; //args[0]; //'DEV' or 'PROD'
String side = "PUBLIC";//args[1]; //'PUBLIC' or 'SECURE'
String file = "D:/db1.sql";//args[2]; // db1.sql
System.out.println("File contents are " + file);
InsertTranslation.init(environment,side,file);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Usage error: java com.dupont.epportal.util.rbGenerator <outputPath> <database> <username> <password>");
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("] EP Portal error: could not complete rb generation");
e.printStackTrace();
}
}
/**
*
**/
private static void run(String file) throws IOException
{
InsertTranslation.openConnection();
try
{
FileInputStream fs1 = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fs1,"UTF-8"));
String curLine = "";
while ((curLine = reader.readLine()) != null)
{
StringTokenizer a = new StringTokenizer(curLine,",");
Vector vTokens = tokens(a);
System.out.println("St:"+a);
try
{
execute(vTokens);
}
catch (SQLException e1)
{
e1.printStackTrace();
}
} // End While
fs1.close();
reader.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
InsertTranslation.closeConnection();
}
/**
*
*
*
**/
private static void execute(Vector list) throws SQLException
{
PreparedStatement pstmt = null;
ResultSet results = null;
pstmt = getConnection().prepareStatement("INSERT INTO testschema.MULTI_LANG_ASSET VALUES (?,?,?,?,?)");
System.out.println(pstmt);
pstmt.setString(1,(String)list.get(0)); //ID
pstmt.setString(2,(String)list.get(1)); //TRADEMARK
pstmt.setString(3,(String)list.get(2)); //TRADEMARK
pstmt.setString(4,(String)list.get(3)); //TRADEMARK
pstmt.setString(5,(String)list.get(4)); //TRADEMARK
pstmt.execute();
}
/**
*
**/
private static Vector tokens(StringTokenizer tokens)
{
Vector vTokens = new Vector();
while(tokens.hasMoreElements())
{
Object temp = tokens.nextToken();
System.out.println("token : " + temp);
vTokens.add(temp);
}
return vTokens;
}
/**
*
**/
private static void openConnection()
{
//Added - START
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
//String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "Sairam9";
//Added - END
String machine = InsertTranslation.server + ":" + InsertTranslation.port + ":" + InsertTranslation.dbase;
if(DBconnection == null)
{
try
{
// Class.forName("oracle.jdbc.driver.OracleDriver");
//MySQL Driver
Class.forName("com.mysql.jdbc.Driver").newInstance();
//DBconnection = DriverManager.getConnection("jdbc racle:thin:@" + machine, InsertTranslation.uname, InsertTranslation.pword);
//Added by Keerthi Kumar Narayan - START
DBconnection = DriverManager.getConnection(url, userName, password);
//Added by Keerthi Kumar Narayan - END
}
catch(Exception e)
{
System.out.println("EXCEPTION penConnection(): " + e.toString());
}
}
}
private static void closeConnection()
{
try
{
if (DBconnection != null)
{
DBconnection.close();
DBconnection = null;
}
}
catch(Exception e)
{
System.out.println("EXCEPTION:closeConnection(): " + e.toString());
}
}
/**
* Devolve um conexão aberta.
**/
private static Connection getConnection()
{
return DBconnection;
}
/**
*
**/
private static void init(String environment, String side, String file) throws IOException
{
InputStream input = ClassLoader.getSystemResourceAsStream(BUNDLE);
Properties properties = new Properties();
properties.load(input);
input.close();
InsertTranslation.server = properties.getProperty(environment + ".server");
InsertTranslation.port = properties.getProperty(environment + ".port");
InsertTranslation.dbase = properties.getProperty(environment + "." + side + ".dbase");
InsertTranslation.uname = properties.getProperty(environment + "." + side + ".uname");
InsertTranslation.pword = properties.getProperty(environment + "." + side + ".pword");
System.out.println("=======================" + environment+ " - " + side + "===============================");
System.out.println("SERVER : " + InsertTranslation.server);
System.out.println("PORT : " + InsertTranslation.port);
System.out.println("DBASE : " + InsertTranslation.dbase);
System.out.println("UNAME : " + InsertTranslation.uname);
System.out.println("PWORD : " + InsertTranslation.pword);
System.out.println("==================================================================");
run(file);
}
}
Thanks,
Keerthi Kumar N
|
Cheers,
Keerthi Kumar N
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26496
|
|
Keerthi,
In the future, please use [code] tags when posting code - it formats it and makes it easier to read. Also, it's helpful to use the smallest possible example that shows the error rather than all your code. The above post is 9 screens on my machine. That's a lot to read. I'm sure you can illustrate the problem in a screen or two. For example, hard code an example that gives an error.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
 |
|
|
subject: How to insert the multi lang database into MySQl
|
|
|