SQL values

This is my first attempt using a db (MYSQL) and I need help.

I have a Primary key in the db as

empid char(5) Primary Key,

in my project I need to know what data type I needs to use. I will be using a scanner to prompt the user to input info for that field.

thnaks

SandyR

PS I tried using a string and got this when I put the info.

Error listing records: Unknown column 'empid' in 'field list'

Message was edited by:

SandyRed

[490 byte] By [SandyReda] at [2007-11-27 3:43:23]
# 1

It is good practice to always have primary keys as integers. Always try to use a separate id from your values. It looks redundant, but when you get into complex data models and many columns, etc... a practice such as this in my experience has been very helpful. The id can be generated from the database, or you can assign it as you please, but not based on user entry. If you were using an Id that is based on user entry, you can compromise your data integrity, where in a case, fat fingers will cause a wrong entry in the tables, which in turn could lead to orphan records in the database.

kdajania at 2007-7-12 8:47:02 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks for the reply...

But This is a class assignment so the data type is preset in the db.

as an char.

when I use the char in java it want to add an (Object) to it.

Any Ideals would be great

here is part of my project.

public static void addRecord(String id, String fname) throws SQLException {

// make variables

Statement stmt = null;

String addstring = "Insert into emptable values('" + id + "', '"

+ fname + "')";

try {

stmt = conn.createStatement();

boolean ret = stmt.execute(addstring);

if (ret) {

System.out.println("Updated " + stmt.getUpdateCount());

}//close if

} catch (SQLException e) {

System.out.println("Error " + e.getMessage());

throw (e);

} finally {

try {

if (stmt != null) {

stmt.close();

}//close if

} catch (SQLException e2) {

System.out

.println("Error freeing resources " + e2.getMessage());

throw (e2);

}//close e2

}//close finally

}//close exception

public static void addToTable() {

// get data from keyboard

String id = null;

String fname;

boolean dupkey = true;

while (dupkey == true) {

System.out.println("Enter Employee's ID");

id = kbd.next();

kbd.nextLine();

dupkey = findRecord(id);

if (dupkey == true) {

System.out.println("Key must be unique - try again");

}

}

System.out.println("Enter first name");

fname = kbd.next();

kbd.nextLine();

try {

addRecord(id, fname);

} catch (SQLException e) {

System.out.println("Error adding record " + e.getMessage());

}

}//close add

sandyr

SandyReda at 2007-7-12 8:47:02 > top of Java-index,Java Essentials,New To Java...
# 3
Sorry, I don't understand what you mean by > when I use the char in java it want to add an > (Object) to it.Is there a specific error message you can supply to be able to help better?
kdajania at 2007-7-12 8:47:02 > top of Java-index,Java Essentials,New To Java...
# 4

Thanks for the reply back..

Here is the code if I had char to my id.

public static void addToTable() {

// get data from keyboard

Object id = null;

also how do I solve this problem.

if Id is a char what about this statement how do I solve this...

Statement stmt = null;

String addstring = "Insert into emptable values('" + id + "', '"+ fname + "', " + lname ',)";

Thanks

sandyR

SandyReda at 2007-7-12 8:47:02 > top of Java-index,Java Essentials,New To Java...
# 5

Statement stmt = null;

String addString = "INSERT INTO emptable VALUES (" + id + ", " + fname + ", " + lname + ")";

...

If your id field is CHAR in the database, then String will be the appropriate JAVA type to use.

kdajania at 2007-7-12 8:47:02 > top of Java-index,Java Essentials,New To Java...
# 6
In the fullness of time, you should stop writing code like that and use PreparedStatement instead. http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html
DrLaszloJamfa at 2007-7-12 8:47:02 > top of Java-index,Java Essentials,New To Java...
# 7
here here. Look into Hibernate as well for future db programming.
kdajania at 2007-7-12 8:47:02 > top of Java-index,Java Essentials,New To Java...
# 8
> here here. Look into Hibernate as well for future db> programming.A bit overkill for an assignment? :)
kajbja at 2007-7-12 8:47:02 > top of Java-index,Java Essentials,New To Java...
# 9
hence the word future : ), def not for assignments.
kdajania at 2007-7-12 8:47:02 > top of Java-index,Java Essentials,New To Java...
# 10

Thanks for the replay.

I also have a place in my program where I can add info to the db.

When I run the project and get this at the end

error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'df',df',5555',' at line 1

Error adding record You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'df',df',5555',)' at line 1

Is this telling me that the two data type don't match, and if so I'm back to square one.

thanks

sandy

SandyReda at 2007-7-12 8:47:02 > top of Java-index,Java Essentials,New To Java...
# 11
What are all the fields in that table?You might have to to specify them in your query.INSERT INTO table (columnName_1, columnName_2) VALUES (val1, val2);
kdajania at 2007-7-12 8:47:02 > top of Java-index,Java Essentials,New To Java...
# 12
> Thanks for the replay.> I also have a place in my program where I can add> info to the db.> When I run the project and get this at the end Read reply #6
kajbja at 2007-7-12 8:47:02 > top of Java-index,Java Essentials,New To Java...
# 13

This is what is in the table on the dbcreate database empdata;

[code]use empdata;

create table empTable (

empid char(5) Primary Key,

firstname varchar(20),

lastname varchar(30),

yearsemploy int,

monthpay float);[/code

sandyR

PS remember I'm just a beginner so sometimes you are talking a foreign lang to me...

Message was edited by:

SandyRed

SandyReda at 2007-7-12 8:47:02 > top of Java-index,Java Essentials,New To Java...
# 14
> PS remember I'm just a beginner so sometimes you are> talking a foreign lang to me...Did you read the link in reply #6? It explains how you should solve your problem.Kaj
kajbja at 2007-7-12 8:47:02 > top of Java-index,Java Essentials,New To Java...
# 15

> This is what is in the table on the dbcreate database

> empdata;

> use empdata;

> create table empTable (

> empid char(5) Primary Key,

> firstname varchar(20),

> lastname varchar(30),

> yearsemploy int,

> monthpay float);[/code

>

> sandyR

> PS remember I'm just a beginner so sometimes you are

> talking a foreign lang to me...

Rather hard to tell you what is wrong without know exactly what code you are using for each post.

The error you posted means something is wrong with the SQL, not java, that you are using.

Based entirely on your first post there is nothing implicitly wrong.

You can start by posting exactly what value you have for addstring by PRINTING it before you use it. Do not post the code - instead print it and post only that.

jschella at 2007-7-21 20:53:48 > top of Java-index,Java Essentials,New To Java...