Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

Hilton Meyer

Greenhorn
+ Follow
since Nov 13, 2003
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Hilton Meyer

NPE
[CODE}
/*
* trackerDAO.java
*
* Created on July 16, 2006, 9:18 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package my.app.db;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
*
* @author John O'Conner
*/
public class trackerDAO {

/**
* Creates a new instance of trackerDAO
*/
public trackerDAO() {
this("DefaultTeamStock");
}

public trackerDAO(String teamStockName) {
this.dbName = teamStockName;

setDBSystemDir();
dbProperties = loadDBProperties();
String driverName = dbProperties.getProperty("derby.driver");
loadDatabaseDriver(driverName);
if(!dbExists()) {
createDatabase();
}

}

private boolean dbExists() {
boolean bExists = false;
String dbLocation = getDatabaseLocation();
File dbFileDir = new File(dbLocation);
if (dbFileDir.exists()) {
bExists = true;
}
return bExists;
}

private void setDBSystemDir() {
// decide on the db system directory
String userHomeDir = System.getProperty("user.dir", ".");
String systemDir = userHomeDir + "/teamstock";
System.setProperty("derby.system.home", systemDir);

// create the db system directory
File fileSystemDir = new File(systemDir);
fileSystemDir.mkdir();
}

private void loadDatabaseDriver(String driverName) {
// load Derby driver
try {
Class.forName(driverName);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}

}

private Properties loadDBProperties() {
InputStream dbPropInputStream = null;
dbPropInputStream = trackerDAO.class.getResourceAsStream("Configuration.properties");
dbProperties = new Properties();
try {
dbProperties.load(dbPropInputStream);
} catch (IOException ex) {
ex.printStackTrace();
}
return dbProperties;
}


private boolean createTables(Connection dbConnection) {
boolean bCreatedTables = false;
Statement statement = null;
try {
statement = dbConnection.createStatement();
statement.execute(strCreateAddressTable);
bCreatedTables = true;
} catch (SQLException ex) {
ex.printStackTrace();
}

return bCreatedTables;
}
private boolean createDatabase() {
boolean bCreated = false;
Connection dbConnection = null;

String dbUrl = getDatabaseUrl();
dbProperties.put("create", "true");

try {
dbConnection = DriverManager.getConnection(dbUrl, dbProperties);
bCreated = createTables(dbConnection);
} catch (SQLException ex) {
}
dbProperties.remove("create");
return bCreated;
}


public boolean connect() {
String dbUrl = getDatabaseUrl();
try {
dbConnection = DriverManager.getConnection(dbUrl, dbProperties);
stmtSaveNewRecord = dbConnection.prepareStatement(strSaveAddress, Statement.RETURN_GENERATED_KEYS);
stmtUpdateExistingRecord = dbConnection.prepareStatement(strUpdateAddress);
stmtGetAddress = dbConnection.prepareStatement(strGetAddress);
stmtDeleteAddress = dbConnection.prepareStatement(strDeleteAddress);

isConnected = dbConnection != null;
} catch (SQLException ex) {
isConnected = false;
}
return isConnected;
}

private String getHomeDir() {
return System.getProperty("user.home");
}

public void disconnect() {
if(isConnected) {
String dbUrl = getDatabaseUrl();
dbProperties.put("shutdown", "true");
try {
DriverManager.getConnection(dbUrl, dbProperties);
} catch (SQLException ex) {
}
isConnected = false;
}
}

public String getDatabaseLocation() {
String dbLocation = System.getProperty("derby.system.home") + "/" + dbName;
return dbLocation;
}

public String getDatabaseUrl() {
String dbUrl = dbProperties.getProperty("derby.url") + dbName;
return dbUrl;
}

/*
public int saveRecord(Address record) {
int id = -1;
try {
stmtSaveNewRecord.clearParameters();

stmtSaveNewRecord.setString(1, record.getLastName());
stmtSaveNewRecord.setString(2, record.getFirstName());
stmtSaveNewRecord.setString(3, record.getMiddleName());
stmtSaveNewRecord.setString(4, record.getPhone());
stmtSaveNewRecord.setString(5, record.getEmail());
stmtSaveNewRecord.setString(6, record.getAddress1());
stmtSaveNewRecord.setString(7, record.getAddress2());
stmtSaveNewRecord.setString(8, record.getCity());
stmtSaveNewRecord.setString(9, record.getState());
stmtSaveNewRecord.setString(10, record.getPostalCode());
stmtSaveNewRecord.setString(11, record.getCountry());
int rowCount = stmtSaveNewRecord.executeUpdate();
ResultSet results = stmtSaveNewRecord.getGeneratedKeys();
if (results.next()) {
id = results.getInt(1);
}

} catch(SQLException sqle) {
sqle.printStackTrace();
}
return id;
}

public boolean editRecord(Address record) {
boolean bEdited = false;
try {
stmtUpdateExistingRecord.clearParameters();

stmtUpdateExistingRecord.setString(1, record.getLastName());
stmtUpdateExistingRecord.setString(2, record.getFirstName());
stmtUpdateExistingRecord.setString(3, record.getMiddleName());
stmtUpdateExistingRecord.setString(4, record.getPhone());
stmtUpdateExistingRecord.setString(5, record.getEmail());
stmtUpdateExistingRecord.setString(6, record.getAddress1());
stmtUpdateExistingRecord.setString(7, record.getAddress2());
stmtUpdateExistingRecord.setString(8, record.getCity());
stmtUpdateExistingRecord.setString(9, record.getState());
stmtUpdateExistingRecord.setString(10, record.getPostalCode());
stmtUpdateExistingRecord.setString(11, record.getCountry());
stmtUpdateExistingRecord.setInt(12, record.getId());
stmtUpdateExistingRecord.executeUpdate();
bEdited = true;
} catch(SQLException sqle) {
sqle.printStackTrace();
}
return bEdited;

}

public boolean deleteRecord(int id) {
boolean bDeleted = false;
try {
stmtDeleteAddress.clearParameters();
stmtDeleteAddress.setInt(1, id);
stmtDeleteAddress.executeUpdate();
bDeleted = true;
} catch (SQLException sqle) {
sqle.printStackTrace();
}

return bDeleted;
}

public boolean deleteRecord(Address record) {
int id = record.getId();
return deleteRecord(id);
}

public List<ListEntry> getListEntries() {
List<ListEntry> listEntries = new ArrayList<ListEntry>();
Statement queryStatement = null;
ResultSet results = null;

try {
queryStatement = dbConnection.createStatement();
results = queryStatement.executeQuery(strGetListEntries);
while(results.next()) {
int id = results.getInt(1);
String lName = results.getString(2);
String fName = results.getString(3);
String mName = results.getString(4);

ListEntry entry = new ListEntry(lName, fName, mName, id);
listEntries.add(entry);
}

} catch (SQLException sqle) {
sqle.printStackTrace();

}

return listEntries;
}

public Address getAddress(int index) {
Address address = null;
try {
stmtGetAddress.clearParameters();
stmtGetAddress.setInt(1, index);
ResultSet result = stmtGetAddress.executeQuery();
if (result.next()) {
String lastName = result.getString("LASTNAME");
String firstName = result.getString("FIRSTNAME");
String middleName = result.getString("MIDDLENAME");
String phone = result.getString("PHONE");
String email = result.getString("EMAIL");
String add1 = result.getString("ADDRESS1");
String add2 = result.getString("ADDRESS2");
String city = result.getString("CITY");
String state = result.getString("STATE");
String postalCode = result.getString("POSTALCODE");
String country = result.getString("COUNTRY");
int id = result.getInt("ID");
address = new Address(lastName, firstName, middleName, phone,
email, add1, add2, city, state, postalCode,
country, id);
}
} catch(SQLException sqle) {
sqle.printStackTrace();
}

return address;
}
*/
public static void main(String[] args) {
trackerDAO db = new trackerDAO();
System.out.println(db.getDatabaseLocation());
System.out.println(db.getDatabaseUrl());
db.connect();
db.disconnect();
}


private Connection dbConnection;
private Properties dbProperties;
private boolean isConnected;
private String dbName;
private PreparedStatement stmtSaveNewRecord;
private PreparedStatement stmtUpdateExistingRecord;
private PreparedStatement stmtGetListEntries;
private PreparedStatement stmtGetAddress;
private PreparedStatement stmtDeleteAddress;

private static final String strCreateAddressTable =
"create table APP.ADDRESS (" +
" ID INTEGER NOT NULL PRIMARY KEY GENERATED
ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)," +
" LASTNAME VARCHAR(30), " +
" FIRSTNAME VARCHAR(30), " +
" MIDDLENAME VARCHAR(30), " +
" PHONE VARCHAR(20), " +
" EMAIL VARCHAR(30), " +
" ADDRESS1 VARCHAR(30), " +
" ADDRESS2 VARCHAR(30), " +
" CITY VARCHAR(30), " +
" STATE VARCHAR(30), " +
" POSTALCODE VARCHAR(20), " +
" COUNTRY VARCHAR(30) " +
")";

private static final String strGetAddress =
"SELECT * FROM APP.ADDRESS " +
"WHERE ID = ?";

private static final String strSaveAddress =
"INSERT INTO APP.ADDRESS " +
" (LASTNAME, FIRSTNAME, MIDDLENAME, PHONE, EMAIL, ADDRESS1, ADDRESS2, " +
" CITY, STATE, POSTALCODE, COUNTRY) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";


private static final String strGetListEntries =
"SELECT ID, LASTNAME, FIRSTNAME, MIDDLENAME FROM APP.ADDRESS " +
"ORDER BY LASTNAME ASC";

private static final String strUpdateAddress =
"UPDATE APP.ADDRESS " +
"SET LASTNAME = ?, " +
" FIRSTNAME = ?, " +
" MIDDLENAME = ?, " +
" PHONE = ?, " +
" EMAIL = ?, " +
" ADDRESS1 = ?, " +
" ADDRESS2 = ?, " +
" CITY = ?, " +
" STATE = ?, " +
" POSTALCODE = ?, " +
" COUNTRY = ? " +
"WHERE ID = ?";

private static final String strDeleteAddress =
"DELETE FROM APP.ADDRESS " +
"WHERE ID = ?";

}
[/CODE]


This is the full code for a class I retrived off the internet. Somehow I got the program working by complete accident but then when I changed something it kicked out the same NPE. I could get it working again as it keeps popping up with a NullPointerException on the line where my properties are assigned:dbProperties.load(dbPropInputStream);. I tried to restart from the beginning as thats how I got it working this morning but to no avail. My confirguration.properties file is in the same directory as the class. I know that it works because I actually got the entire program to run through. I tried debugging but I keep coming unstuck because I can't see where I'm going wrong. The error is occurs when in the loadDBProperties method because the dbPropInputStream does not obtain the information form the properties file.

As for setting the class path I think that the program is setting the classpath with this code:



The following information is in the properties file:

I'd like to get this working as its my first attempts at getting any database connection in java and I can see that by using this class I could tweak it to fit into any other future application.
I'm trying to extract a table off a web page so that I can place the data into a database. I've been trying hopelessly to use the HTMLEditorKit by iterating and even trying to get each element but I keep running into a brick wall. When using an iterator it returns null for HTML.Tag.TD even though there is a table present. I've read that there are some problems with tables in the editor so you may be able to point me in the right direction. I'm really at my the end of my wits so any help would be appreciated.
15 years ago
I would just like your opinions on Sun Java Studio Enterprise & Oracle JDeveloper 10g. I would like to use one of these products and am looking developing applications with Java for Oracle. Really quite new to developing java applications and only just got past the basic's but looking ahead I would like to get into a career with both Oracle and Java thus the question about which is better. I would like to start using either of these as soon as possible as this will enable me to continue to more advanced java with an IDE that is a bit more advanced.

Thanx
Hi there,

Just need a bit of an explanation. about the import statements of the Swing classes. Why do I have to include both

does the java.awt.*; not automatically add the event statement with the (*) the the end of the awt in the first line.
15 years ago
Thanks to evryone,

I fixed the problem by set the variables permanently in the Enviroment Variables in System Settings. Evrytime I was setting the paths up they would work but once I closed the command prompt window they would be reset to null. I'm not sure if I was doing it incorrectly but now they are permanently set up so there's no more hassles.

Thank you very much
15 years ago


This code throws a an AuthenicationFailedException as soon as it gets to store.connect();
I'm working through an article and am just trying to get the program working so that I could better understand James so some of this code is a little beyond me at this stage. The error says that it failed in com.sun.mail.pop3.POP3Store. I checked the users on the James server and they are there and their passwords are the same as set up in the tutorial.
16 years ago
I tried to use the javac command but the system said that this was not recognised. After reading a bit further in the article they suggested copying the jar's into the folder. It's no the best solution but it solved the problem for now. I would still like a solution so that in future I would not run into this problem.
16 years ago
do you need a sorting algorithm or the whole program??
16 years ago
I solved the problem by copying the jar's into the jre/ext file. I would still like to know if there are other solutions.
16 years ago
As I can understand the delimiters used by StringTokenizer there is the default delimiters which are whitespace characters(blank,new line or any other separators that are whitespace if you sere to print them on paper) and this is set up with something like:

If you wanted to specify which each separator is you would need to use the constructor where you can specify the delimiters or separators that you can make up:

This part in inverted comma's: " \n%." are your specified delimiters. the first is a space followed by \n which is new line then % and .
These are all your own separators and you can use basically any symbol as a separator or delimiter as long as you specify it when initialising the variable(in this case sentence)
Hope this helps but if you need something's unclear give me a shout because it has helped me a bit better in understanding the StringTokeniser Class.

[ January 25, 2006: Message edited by: Hilton Meyer ]
[ January 25, 2006: Message edited by: Hilton Meyer ]
16 years ago
I'm wondering whether its just me or if others have had trouble with this. I've searched the Interent and came up with complicated answer's. I'm trying to go through a JavaMail&James tutorial but I can't even set up the JavaMail API. I try to Set the CLASSPATH which seems to work because when I "echo %CLASSPATH" it comes back with the results that I input. Once I compile the program however the compiler tells me that: package javax.mail does not exist. I'm not sure whether its the setup problem or something else. I've placed the unziped folder of JavaMail into a folder called C:\JAMES_HOME\javamail-1.4ea. After this I added the mail.jar to the CLASSPATH like this: set CLASSPATH=%CLASSPATH%;c:\JAMES_HOME\javamail-1.4ea\mail.jar;. I have two questions:
1)Is the JavaMail API setup in the CLASSPATH and can I use it like this?
2)Is the compile error connected to this problem?

Here is the code of the program:

[ January 25, 2006: Message edited by: Hilton Meyer ]
16 years ago
do you by any chance have an address that has all the formats for the months, days and years. such as month MMM, ect.
Thnx
18 years ago
Hi there,
Just trying to get this simple date program to work but it keeps
throwing the exception. I've tried swopping the month, respelling the month
and even restructuring the entire string ut nothing!! Please lend your input to this
simple peasant of little knowledge

18 years ago
Hit there,
Does anybody have a comprehesive example of a database connection that I may take a look at. Just want to learn the ropes with databases so would like to work with something better than the examples I've been working through in text books.
Shot a dot
I figured out the problem. I was trying to use double quotes instead of simply single quotes. I come from a VB background so just got the syntax a bit wrong