This is my first attempt in SQL and PreparedStateme nt
I have add the PreparedStateme nt and I'm not to sure if I'm doing it correctly.
When I do a test run on Choose 1 ,or 2 I get this error.
Error - com.mysql.jdbc. Statement
here is my code below
[CODE=java]public class DBAssign {
/**
* @param args
*/
static Scanner kbd;
static Connection conn = null;
//adding records...
public static void addRecord(Strin g id, String fname, String lname,
String street, String city, String state, String zip,
String hphone, String ophone, String deptasg, int yrsemp,
float mtpy) throws SQLException {
// make variables
PreparedStateme nt statement = null;
String addstring = "Insert into emptable values('" + id + "', '"
+ fname + "', " + lname + ", '" + street + "'" + city + "',"
+ state + "'," + zip + "'," + hphone + "'," + ophone + "'"
+ deptasg + "'," + yrsemp + "'," + mtpy + "',)";
try {
statement=conn. prepareStatemen t("INSERT into user values(?,?)");
statement = (PreparedStatem ent) conn.createStat ement();
boolean ret = statement.execu te(addstring);
if (ret) {
System.out.prin tln("Updated " + statement.getUp dateCount());
}//close if
} catch (SQLException e) {
System.out.prin tln("Error " + e.getMessage()) ;
throw (e);
} finally {
try {
if (statement != null) {
statement.close ();
}//close if
} catch (SQLException e2) {
System.out
.println("Error freeing resources " + e2.getMessage() );
throw (e2);
}//close e2
}//close finally
}//close exception
//add to the record
public static void addToTable() {
// get data from keyboard
String id = null;
String fname; // =null;
String lname;
String street;
String city;
String state;
String zip;
String hphone;
String ophone;
String deptasg;
int yrsemp;
float mtpy;
boolean dupkey = true;
while (dupkey == true) {
System.out.prin tln("Enter Employee ID");
id = kbd.next();
kbd.nextLine();
dupkey = findRecord(id);
if (dupkey == true) {
System.out.prin tln("Key must be unique - try again");
}
}
System.out.prin tln("Enter first name");
fname = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter last name");
lname = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter street name");
street = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter city");
city = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter state");
state = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter zip code");
zip = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter home phone number");
hphone = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter office phone");
ophone = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter dept name");
deptasg = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter years employeed");
yrsemp = kbd.nextInt();
kbd.nextLine();
System.out.prin tln("Enter month pay");
mtpy = kbd.nextFloat() ;
kbd.nextLine();
try {
addRecord(id, fname, lname, street, city, state, zip, hphone,
ophone, deptasg, yrsemp, mtpy);
} catch (SQLException e) {
System.out.prin tln("Error adding record " + e.getMessage()) ;
}
}//close add
//List records
public static void listRecords() {
PreparedStateme nt statement = null;
ResultSet rs = null;
try {
statement = (PreparedStatem ent) conn.createStat ement();
rs = statement
.executeQuery(" Select empid, firstname, lastname, street, city, state, zip, homephone, officephone, department, yearsemploy, monthpay");
if (rs != null) {
while (rs.next()) {
System.out.prin tln(rs.getStrin g("empid") + " - "
+ rs.getString("f irstname") + " - "
+ rs.getInt("last name") + " - "
+ rs.getString("s treet") + " - "
+ rs.getString("c ity") + " - "
+ rs.getString("s tate") + " - "
+ rs.getString("z ip") + " - "
+ rs.getString("h phone") + " - "
+ rs.getString("o phone") + " - "
+ rs.getString("d epartment") + " - "
+ rs.getString("y earsemploy") + " - "
+ rs.getString("m onthpay"));
}// close the while loop
}// close the if loop
} catch (SQLException e) {
System.out.prin tln("Error listing records: " + e.getMessage()) ;
} finally {
try {
if (statement != null) {
statement.close ();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e2) {
System.out.prin tln("Error freeing resource" + e2.getMessage() );
}
}//close finally
}//close list
//Find a record
public static boolean findRecord(Stri ng empid) {
boolean retval = true;
PreparedStateme nt statement = null;
ResultSet rs = null;
int countemps = 0;
try {
statement = (PreparedStatem ent) conn.createStat ement();
rs = statement
.executeQuery(" Select count(*) as num from emptable where empid = '"
+ empid + "'");
rs.next();
if (rs == null) {
retval = false;
} else {
countemps = rs.getInt(1);
}
// System.out.prin tln("Count "+countrecs );
if (countemps <= 0) {
retval = false;
}
} catch (SQLException e) {
System.out.prin tln("Error finding Employees record "
+ e.getMessage()) ;
} finally {
try {
if (statement != null) {
statement.close ();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e2) {
System.out.prin tln("Error freeing resource " + e2.getMessage() );
retval = true;
}
}//close finally
return retval;
}//close find
public static void showMenu() {
System.out.prin tln("---------------");
System.out.prin tln("1. List employees records");
System.out.prin tln("2. Add employees record");
System.out.prin tln("3. Update employees record");
System.out.prin tln("4. Delete employees record");
System.out.prin tln("5. Exit");
}
public static int getOption() {
int optn = 99;
while (optn > 5 || optn < 1) {
System.out.prin tln("---");
System.out.prin tln("Enter option");
System.out.prin t("===>");
optn = kbd.nextInt();
}//close while
return optn;
}
public static void main(String[] args) {
int optn = 99;
String host = "localhost" ;
String database = "empdata";
String user = "root";
String pass = "";
// make keyboard object
kbd = new Scanner(System. in);
String connstring = "jdbc:mysql ://" + host + "/" + database + "?user="
+ user + "&password= " + pass;
try {
Class.forName(" com.mysql.jdbc. Driver").newIns tance();
conn = DriverManager.g etConnection(co nnstring);
while (optn != 5) {
showMenu();
optn = getOption();
switch (optn) {
case 1:
listRecords();
break;
case 2:
addToTable();
break;
case 3:
updateRecord();
break;
case 4:
deleteRecord();
case 5:
System.out.prin tln("OK - later Come back soon");
default:
break;
}//close switch
}//close while
} catch (ClassNotFoundE xception e1) {
System.out.prin tln("ERROR - Class not found " + e1.getMessage() );
} catch (SQLException e2) {
System.out.prin tln("ERROR - " + e2.getMessage() );
System.out.prin tln("ERROR - " + e2.getSQLState( ));
} catch (Exception e3) {
System.out.prin tln("Error - " + e3.getMessage() );
}
}//close main
}//close class[/CODE]
Any help would be great.
Red
I have add the PreparedStateme nt and I'm not to sure if I'm doing it correctly.
When I do a test run on Choose 1 ,or 2 I get this error.
Error - com.mysql.jdbc. Statement
here is my code below
[CODE=java]public class DBAssign {
/**
* @param args
*/
static Scanner kbd;
static Connection conn = null;
//adding records...
public static void addRecord(Strin g id, String fname, String lname,
String street, String city, String state, String zip,
String hphone, String ophone, String deptasg, int yrsemp,
float mtpy) throws SQLException {
// make variables
PreparedStateme nt statement = null;
String addstring = "Insert into emptable values('" + id + "', '"
+ fname + "', " + lname + ", '" + street + "'" + city + "',"
+ state + "'," + zip + "'," + hphone + "'," + ophone + "'"
+ deptasg + "'," + yrsemp + "'," + mtpy + "',)";
try {
statement=conn. prepareStatemen t("INSERT into user values(?,?)");
statement = (PreparedStatem ent) conn.createStat ement();
boolean ret = statement.execu te(addstring);
if (ret) {
System.out.prin tln("Updated " + statement.getUp dateCount());
}//close if
} catch (SQLException e) {
System.out.prin tln("Error " + e.getMessage()) ;
throw (e);
} finally {
try {
if (statement != null) {
statement.close ();
}//close if
} catch (SQLException e2) {
System.out
.println("Error freeing resources " + e2.getMessage() );
throw (e2);
}//close e2
}//close finally
}//close exception
//add to the record
public static void addToTable() {
// get data from keyboard
String id = null;
String fname; // =null;
String lname;
String street;
String city;
String state;
String zip;
String hphone;
String ophone;
String deptasg;
int yrsemp;
float mtpy;
boolean dupkey = true;
while (dupkey == true) {
System.out.prin tln("Enter Employee ID");
id = kbd.next();
kbd.nextLine();
dupkey = findRecord(id);
if (dupkey == true) {
System.out.prin tln("Key must be unique - try again");
}
}
System.out.prin tln("Enter first name");
fname = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter last name");
lname = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter street name");
street = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter city");
city = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter state");
state = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter zip code");
zip = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter home phone number");
hphone = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter office phone");
ophone = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter dept name");
deptasg = kbd.next();
kbd.nextLine();
System.out.prin tln("Enter years employeed");
yrsemp = kbd.nextInt();
kbd.nextLine();
System.out.prin tln("Enter month pay");
mtpy = kbd.nextFloat() ;
kbd.nextLine();
try {
addRecord(id, fname, lname, street, city, state, zip, hphone,
ophone, deptasg, yrsemp, mtpy);
} catch (SQLException e) {
System.out.prin tln("Error adding record " + e.getMessage()) ;
}
}//close add
//List records
public static void listRecords() {
PreparedStateme nt statement = null;
ResultSet rs = null;
try {
statement = (PreparedStatem ent) conn.createStat ement();
rs = statement
.executeQuery(" Select empid, firstname, lastname, street, city, state, zip, homephone, officephone, department, yearsemploy, monthpay");
if (rs != null) {
while (rs.next()) {
System.out.prin tln(rs.getStrin g("empid") + " - "
+ rs.getString("f irstname") + " - "
+ rs.getInt("last name") + " - "
+ rs.getString("s treet") + " - "
+ rs.getString("c ity") + " - "
+ rs.getString("s tate") + " - "
+ rs.getString("z ip") + " - "
+ rs.getString("h phone") + " - "
+ rs.getString("o phone") + " - "
+ rs.getString("d epartment") + " - "
+ rs.getString("y earsemploy") + " - "
+ rs.getString("m onthpay"));
}// close the while loop
}// close the if loop
} catch (SQLException e) {
System.out.prin tln("Error listing records: " + e.getMessage()) ;
} finally {
try {
if (statement != null) {
statement.close ();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e2) {
System.out.prin tln("Error freeing resource" + e2.getMessage() );
}
}//close finally
}//close list
//Find a record
public static boolean findRecord(Stri ng empid) {
boolean retval = true;
PreparedStateme nt statement = null;
ResultSet rs = null;
int countemps = 0;
try {
statement = (PreparedStatem ent) conn.createStat ement();
rs = statement
.executeQuery(" Select count(*) as num from emptable where empid = '"
+ empid + "'");
rs.next();
if (rs == null) {
retval = false;
} else {
countemps = rs.getInt(1);
}
// System.out.prin tln("Count "+countrecs );
if (countemps <= 0) {
retval = false;
}
} catch (SQLException e) {
System.out.prin tln("Error finding Employees record "
+ e.getMessage()) ;
} finally {
try {
if (statement != null) {
statement.close ();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e2) {
System.out.prin tln("Error freeing resource " + e2.getMessage() );
retval = true;
}
}//close finally
return retval;
}//close find
public static void showMenu() {
System.out.prin tln("---------------");
System.out.prin tln("1. List employees records");
System.out.prin tln("2. Add employees record");
System.out.prin tln("3. Update employees record");
System.out.prin tln("4. Delete employees record");
System.out.prin tln("5. Exit");
}
public static int getOption() {
int optn = 99;
while (optn > 5 || optn < 1) {
System.out.prin tln("---");
System.out.prin tln("Enter option");
System.out.prin t("===>");
optn = kbd.nextInt();
}//close while
return optn;
}
public static void main(String[] args) {
int optn = 99;
String host = "localhost" ;
String database = "empdata";
String user = "root";
String pass = "";
// make keyboard object
kbd = new Scanner(System. in);
String connstring = "jdbc:mysql ://" + host + "/" + database + "?user="
+ user + "&password= " + pass;
try {
Class.forName(" com.mysql.jdbc. Driver").newIns tance();
conn = DriverManager.g etConnection(co nnstring);
while (optn != 5) {
showMenu();
optn = getOption();
switch (optn) {
case 1:
listRecords();
break;
case 2:
addToTable();
break;
case 3:
updateRecord();
break;
case 4:
deleteRecord();
case 5:
System.out.prin tln("OK - later Come back soon");
default:
break;
}//close switch
}//close while
} catch (ClassNotFoundE xception e1) {
System.out.prin tln("ERROR - Class not found " + e1.getMessage() );
} catch (SQLException e2) {
System.out.prin tln("ERROR - " + e2.getMessage() );
System.out.prin tln("ERROR - " + e2.getSQLState( ));
} catch (Exception e3) {
System.out.prin tln("Error - " + e3.getMessage() );
}
}//close main
}//close class[/CODE]
Any help would be great.
Red
Comment