Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

reading .sql file using java

843859Jul 30 2008 — edited Nov 20 2014
hi all,
i have a query regarding .sql file
my requirement is to import database tables using java
i have done this by reading the .sql file using java
the code is as follows
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class SqlScript
{

	public final static char QUERY_ENDS = ';';

	private File script;

	private Connection con=null;

	private Statement stat;

	/**
	* @param args
	* @throws SQLException
	*/

	public SqlScript(String scriptFileName) throws SQLException
	{
		script = new File(scriptFileName);
		try 
		{
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection("jdbc:mysql://localhost/bms","root","");
			stat=con.createStatement();
		} 
		catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		}
	}
	public static void main(String[] args) 
	{
		try
		{
			SqlScript sqlScript = new SqlScript("C:/Program Files/Apache Software Foundation/Tomcat 5.0/webapps/LastestCopy/bms.sql");
			sqlScript.loadScript();
			sqlScript.execute();
		}
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	protected void loadScript() throws IOException, SQLException 
	{
		BufferedReader reader = new BufferedReader(new FileReader(script));
		String line;
		StringBuffer query = new StringBuffer();
		boolean queryEnds = false;
		while ((line = reader.readLine()) != null) 
		{
			if (isComment(line))
				continue;
			queryEnds = checkStatementEnds(line);
			query.append(line);
			if (queryEnds)
			{
				System.out.println("query->"+query);
				stat.addBatch(query.toString());
				query.setLength(0);
			}
		}
	}
	private boolean isComment(String line) 
	{
		if ((line != null) && (line.length() > 0))
			return (line.charAt(0) == '#');
		return false;
	}
	public void execute() throws IOException, SQLException 
	{
		stat.executeBatch();
	}
	private boolean checkStatementEnds(String s) 
	{
		return (s.indexOf(QUERY_ENDS) != -1);
	}
}
it worked well and fine
my problem is if the tables contain any images then the problem rises
the .sql file consisits of some byte code or so which is not readable instead of the image
so how can we handle such a .sql file using java
anyone please help me in this regard
thanks in advance
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 27 2008
Added on Jul 30 2008
1 comment
4,421 views