めも

技術メモとその他

normal way to save dbdata in files


package homeWork;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.dbunit.ext.postgresql.*;

public class dbTest {
public static void main(String[] args) throws Exception {
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;


String url = "jdbc:postgresql://localhost:5432/prototype";
String user = "tk";
String password = "";
try{
Class.forName("org.postgresql.Driver");

conn = DriverManager.getConnection(url, user, password);
FileOutputStream stream = new FileOutputStream("/Users/tk/foo_selects.json");

stmt = conn.createStatement();
String sql = "select memo from foo where id = 2";
rset = stmt.executeQuery(sql);


while(rset.next()){
stream.write(rset.getBytes(1));
}

}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e){
e.printStackTrace();
}
finally {
try {
if(rset != null)rset.close();
if(stmt != null)stmt.close();
if(conn != null)conn.close();
}
catch (SQLException e){
e.printStackTrace();
}
}
}
}