How to construct my finished program?
Hi all,
I have asked a few questions on here recently, seeking answers to complete a task where I read from a database and then write the results, as fixed length values, to a text file.
I have successfully acheived this on a test scale, thanks to all your help.
However,
What I need to deliver is a small GUI that the user will use to choose which of the 3 QUERIES to run (to date I have generated a program that uses one of the Queries)
Each one will have a different select statement with a different number/format of fields.
Each of them will need to write to a text file (with a different file name)
My questions are as follows:
- I have one datasource that all 3 will need to connect to.
How would I specify it once to avoid needless duplication?
- How would I construct it so that I have a simple JFrame where the user chooses which program to run?
here is what I have for one of the programs:
Code:
import java.sql.*;
import java.io.*;
import java.io.FileWriter;
class PSResData {
public static String padRight(String s, int n) {
return String.format("%1$-" + n + "s", s).substring(0, n);
}
public static String padLeft(String s, int n) {
// Method to pad out the database fields
// Call for each field with the specified length
return String.format("%1$#" + n + "s", s);
}
public static void main(String args[]) throws SQLException, IOException {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
int count = 0;
String serverName = "francisca";
int port = 1521;
String user = "niku";
String password = "niku";
String SID = "nikuuat";
String URL = "jdbc:oracle:thin:@" + serverName + ":" + port + ":" + SID;
Connection conn = DriverManager.getConnection(URL, user, password);
String SQL = "SELECT '25000', DECODE(IS_ACTIVE, 1, 'A', 'I'), UNIQUE_NAME, " +
" FIRST_NAME||LAST_NAME FROM NIKU.SRM_RESOURCES ";
String newLine = System.getProperty("line.separator");
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(SQL);
FileWriter fout = new FileWriter("c:\\test.txt"); //Create new file stream
while (rs.next()) { //cycle through result set
count++;
String counter = ""+count;
fout.write(
padRight(rs.getString(1), 5) +
padRight(rs.getString(2), 1) +
padRight(rs.getString(3), 11) +
padRight(rs.getString(4), 60) + newLine
);
}
fout.close(); //close file stream
System.out.println("Complete");
stat.close();
conn.close();
}
}
Thank you all again for your assistance,
Matt