import java.sql.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class retrieveexcel1 {
//connecting to database using driver jdbc odbc
public static void connectToDatabase() throws SQLException, InstantiationException, IllegalAccessException, IOException
{
// Attempt to load database driver
try
{
// Load Sun's jdbc-odbc driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
System.out.println("loaded");
}
catch (ClassNotFoundException cnfe) // driver not found
{
System.err.println ("Unable to load database driver");
System.err.println ("Details : " + cnfe);
System.exit(0);
}
}
//setting up and getting connection to excel sheets
public static Connection setconnection() throws SQLException
{
Connection conn1 = DriverManager.getConnection("jdbc:odbc:trialchr1mergedhg18andhg19","","");
return conn1;
}
public static Connection getconnection() throws SQLException
{
return setconnection();
}
public static Connection setconnection2() throws SQLException
{
Connection conn2 = DriverManager.getConnection("jdbc:odbc:chr1tobemapped","","");
return conn2;
}
public static Connection getconnection2() throws SQLException
{
return setconnection2();
}
//return conn1;
//}
//querying and writing result set to a new file
public static void retrieve( Connection c,Connection c1) throws SQLException, IOException {
Connection conn1=c;
Connection conn2= c1;
String query="Select chrom,hg18strt,hg18end,hg19strt,hg19end from [sheet1$],[sheet2$]"
+"where hg18strt='491'";
Statement st = (Statement) conn1.createStatement();
ResultSet rs=((java.sql.Statement) st).executeQuery(query);
System.out.println("done");
String home=System.getProperty("user.dir")+"/";
System.out.println(home);
File f = new File(home+"merged.txt");
System.out.println("created file");
FileWriter fstream = new FileWriter(f);
BufferedWriter out = new BufferedWriter (fstream);
while(rs.next()){
out.write(rs.getString("hg18strt"));
out.newLine();
}
out.close();
rs.close();
st.close();
}
//closing both connecton
public static void closeConnection(java.sql.Connection c,java.sql.Connection c1) {
try {
c.close();
c1.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//main class
public static void main(String[] args) throws SQLException, IOException, InstantiationException, IllegalAccessException {
retrieveexcel1 re = new retrieveexcel1();
re.connectToDatabase();
java.sql.Connection c =getconnection();
java.sql.Connection c1 =getconnection2();
System.out.println("connecting");
retrieve(c,c1);
closeConnection(c,c1);
System.out.println("connected");
}
}
DEar Rob,
I understood what you tried telling me that i cant do this way but i really don't get it how to do it.Can you explain me step by step starting with my 2 excel files. as these files some of it was experimentally generated and not just from database. I did'nt get the part of mysql server because here i did not try connecting it to mysql server