Re: Database and java.sql.SQLException Questions




"zhah99 via JavaKB.com" wrote...

Well Integer was definitly one of my problems, I have the CreateDatabase
running now. I am working on the SalesClient. I am getting a
NullPointerException in the addNewSalesman method below. I think it might
have to do with the way I am inserting the data into the database, is this
correct? Am I do something wrong with the quotes?

Why not just test with some values, as if..

salesmanname = "John";
level = .15;

String insertsalesman2 = "INSERT INTO Salesman VALUES(" + salesmanname +
"," + "''" + level + ")";

....would become:

String insertsalesman2 =
"INSERT INTO Salesman VALUES(John,''.15)";

....which probably isn't what you want...

I would suggest you start using PreparedStatements instead, which would
simplify things, and make it a bit safer. That would insert those irritating
quotes by itself, and only when needed.

If I recall, a salesman really had three fields...

I expect you have your statements as instance variables somewehere else, so
if you continue with that solution, you could have them prebuilt there as
well...

=== in the class definition =======================

CallableStatement insertSalesmanStmt = null;

=== in the initalization code of the statements ===

String insertsalesman =
"INSERT INTO Salesman VALUES(?,?,?)";

insertSalesmanStmt =
connection.prepareCall (insertSalesman);

=== in addNewSalesman =============================

String name = ...
int id = ...
String type = ...

insertSalesmanStmt.setString(1, name);
insertSalesmanStmt.setInt(2, id);
insertSalesmanStmt.setString(3, type);

insertSalesmanStmt.execute();

===================================================

....unless you have changed the declaration of tables since before.

It would anyway show how you could approach it.

// Bjorn A




Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
.