David Cole

Greenhorn
+ Follow
since Mar 07, 2001
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by David Cole

Heres a sample that basically does just what you are looking for:
import java.util.*;
import java.io.*;
public class FileToArray {
public FileToArray() {
}
public String[] doIt(){
String fileName = "D:\\file.txt";
ArrayList lineList = new ArrayList();
BufferedReader reader = null;
try{
reader= new BufferedReader( new FileReader(new File(fileName)));
}
catch(FileNotFoundException fnfe){
System.out.println("Unable to find the file: " + fileName);
}
try{
String currentLine = "";
while( (currentLine = reader.readLine()) != null)
lineList.add(currentLine);
}
catch(IOException ioe){
ioe.printStackTrace();
}
finally{
try{
if(reader != null)
reader.close();
}
catch(IOException ioe) { /* Do Nothing */ }
}
return (String[])lineList.toArray(new String[0]);
}
public static void main(String[] args) {
FileToArray fileToArray = new FileToArray();
String[] lines = fileToArray.doIt();
if(lines != null){
System.out.println("Printing file contents:");
for(int i = 0; i < lines.length; i++){
System.out.println(lines[i]);
}
}
}
}
23 years ago
My first guess what be that you are not giving the full package and class name For Example:
If you have your class in the package:
com.mypackage;
and the class name is: TestClass
You should be using the command:
java com.mypackage.TestClass
Hope this helps.
23 years ago
The problem is simple really..but often hard to remember:
You are attempting to cast a Mammal to a Horse.
What you have to remember is that you can only cast down the tree of inheritance. Which really makes sense.
Let me put it this way, you know that a Horse is a Mammal (Just like in the real world). This is casting up the inheritance tree.
On the other hand, you cannot guarantee that a Mammal is a Horse.
It could be a Monkey, a Human, or a Dog!
This would be casting down the tree, and is invalid.
A more technical example would be that the Horse object is really a composite of inheritance - it contains all that the Mammal contains PLUS any new methods,attributes of a Horse. Therefore, when you cast up to a Mammal, you are really only restricting the view of the object to that of the Mammal - which the Horse is anyway!
23 years ago
For SQL statements that are executed repeatedly, using a PreparedStatement object would almost always be faster than using a Statement object. This is because creating a PreparedStatement object by explicitly giving the SQL statement causes the statement to be precompiled within the database immediately. Thus, when the PreparedStatement is later executed, the DBMS does not have to recompile the SQL statement and prepared an execution plan - it simply runs the statement.
Typically, PreparedStatement objects are used for SQL statements that take parameters. However, they can also be used with repeatedly executed SQL statements that do not accept parameters.
Prepared Statements aren't actually compiled, but they are bound by the JDBC driver. Depending on the driver, Prepared Statements can be a lot faster - if you re-use them. Some drivers bind the columns you request in the SQL statement. When you execute Connection.prepareStatement(), all the columns bindings take place, so the binding overhead does not occur each time you run the Prepared Statement.
Example:
import java.sql.*;
import java.util.Properties;
public class JDBC_PreparedStatement {
public static void main(java.lang.String[] args)
throws Exception
{
String[] values = {"VAL1", "VAL2", "VAL3"};
Connection conn = null;
ResultSet rs = null;
try {
// Register JDBC Driver and connect to DB.
Class.forName("oracle.jdbc.driver.OracleDriver");
c = DriverManager.getConnection("jdbc racle:thin:@150.180.120.200:1500:myDB");

// Example using simple Statement objects:
Statement s = conn.createStatement();

// Each statement gets prepared and executed each time.
for (int i = 0; i < values.length; i++) {
rs = s.executeQuery("SELECT COUNT(*) FROM TABLE1" +
" WHERE TABLE_OWNER = '" + values[i] + "'");
if (rs.next())
System.out.println(values[i] + " owns " + rs.getInt(1) + " tables.");
else
System.out.println(values[i] + " doesn't own any tables on the system.");
}

// Same example using a PreparedStatement
// The statement gets prepared at the time that it is constructed.
PreparedStatement ps = conn.prepareStatement("SELECT COUNT(*) FROM TABLE1S" +
" WHERE TABLE_OWNER = ?");

// Only the execution phase is done later. The prepare work is reused.
for (int i = 0; i < values.length; i++) {
ps.setString(1, values[i]);
rs = ps.executeQuery();
if (rs.next())
System.out.println(values[i] + " owns " + rs.getInt(1) + " tables.");
else
System.out.println(values[i] + " doesn't own any tables on the system.");
}
} finally {
if (conn != null)
conn.close(); // Connection close will close the statements as well.
}
}
}
Good Luck.
24 years ago
Resin is also a good free web/application server
It can be found at http://www.caucho.com
24 years ago