R
Ryan
I am having trouble getting my prepared statements to work.
I have a DAO and a test class to call it.
I think this means i'm passing a null parameter, but I checked with
system.out.println. I dont have any nulls?
SQLException caught!
java.sql.SQLException: Expected to find "an identifier" instead found "?" at
pos
ition 21.
at com.pointbase.net.netJDBCPrimitives.handleResponse(Unknown Source)
at com.pointbase.net.netJDBCPrimitives.handleJDBCObjectResponse(Unknown
Source)
at com.pointbase.net.netJDBCConnection.prepareStatement(Unknown Source)
at contactDAO.setContact(contactDAO.java:81)
at daoTest.main(daoTest.java:14)
Press any key to continue . . .
import java.sql.*;
public class contactDAO {
private static String SELECTQUERY = "select * from contact";
private String SELECTNAME = "SELECT * FROM CONTACT WHERE NAME = ?";
private String INSERTNAME = "INSERT INTO CONTACT (?, ?, ?, ?, ?,?)";
private static String DBCONNECTION =
"jdbcointbase:server://localhost:9092/contacts";
private static String USERNAME = "CONTACT";
private static String PASSWORD = "CONTACT";
public contactDAO () {
try {
Class.forName ( "com.pointbase.jdbc.jdbcUniversalDriver" );
} catch (Exception e) {
System.out.println("ERROR: failed to load pointbase JDBC
driver.");
e.printStackTrace();
return;
}
}
public static void main(String[] args) {
try {
Class.forName ( "com.pointbase.jdbc.jdbcUniversalDriver" );
} catch (Exception e) {
System.out.println("ERROR: failed to load pointbase JDBC
driver.");
e.printStackTrace();
return;
}
try {
Connection c =
DriverManager.getConnection(DBCONNECTION,USERNAME,PASSWORD);
Statement query = c.createStatement();
ResultSet result = query.executeQuery(SELECTQUERY);
System.out.println("------- Content of the Contact
Table: ------------\n");
while(result.next()) {
System.out.println("\t" + result.getString("NAME") +
"\t" + result.getString("STREET") +
"\t" + result.getString("CITY"));
}
}
catch(SQLException e) {
System.err.println("SQLException caught!");
e.printStackTrace();
}
}
public ResultSet getContact (String name) {
ResultSet rs = null;
try {
Connection c =
DriverManager.getConnection(DBCONNECTION,USERNAME,PASSWORD);
PreparedStatement ps = c.prepareStatement(SELECTNAME);
ps.setString(1,name);
rs = ps.executeQuery();
} catch(SQLException e) {
System.err.println("SQLException caught!");
e.printStackTrace();
}
return rs;
}
public int setContact (String name, String street, String city, String
state, int zipcode, String phone) {
int rowcount = 0;
try {
Connection c =
DriverManager.getConnection(DBCONNECTION,USERNAME,PASSWORD);
PreparedStatement ps = c.prepareStatement(INSERTNAME);
ps.setString(1,name); ps.setString(2,street);
ps.setString(3,city); ps.setString(4,state);
ps.setInt(5,zipcode); ps.setString(6,phone);rowcount
= ps.executeUpdate();
} catch(SQLException e) {
System.err.println("SQLException caught!");
e.printStackTrace();
}
return rowcount;
}
}
-----------------------------------------------------------------
and now the test script...
import java.sql.*;
public class daoTest {
public static void main(String[] args) {
int rowcount = 0;
ResultSet rs = null;
try {
contactDAO cd = new contactDAO();
cd.setContact("RALPH","123 My
Street","Reston","Virginia",20194,"(666)666-6666");
cd.getContact("RALPH");
/* while(rs.next()) {
System.out.println("\t" + rs.getString("NAME") +
"\t" + rs.getString("STREET") +
"\t" + rs.getString("CITY"));
}*/
} catch (Exception e) {
System.out.println("ERROR: failed to load pointbase JDBC
driver.");
e.printStackTrace();
return;
}
}
}
I have a DAO and a test class to call it.
I think this means i'm passing a null parameter, but I checked with
system.out.println. I dont have any nulls?
SQLException caught!
java.sql.SQLException: Expected to find "an identifier" instead found "?" at
pos
ition 21.
at com.pointbase.net.netJDBCPrimitives.handleResponse(Unknown Source)
at com.pointbase.net.netJDBCPrimitives.handleJDBCObjectResponse(Unknown
Source)
at com.pointbase.net.netJDBCConnection.prepareStatement(Unknown Source)
at contactDAO.setContact(contactDAO.java:81)
at daoTest.main(daoTest.java:14)
Press any key to continue . . .
import java.sql.*;
public class contactDAO {
private static String SELECTQUERY = "select * from contact";
private String SELECTNAME = "SELECT * FROM CONTACT WHERE NAME = ?";
private String INSERTNAME = "INSERT INTO CONTACT (?, ?, ?, ?, ?,?)";
private static String DBCONNECTION =
"jdbcointbase:server://localhost:9092/contacts";
private static String USERNAME = "CONTACT";
private static String PASSWORD = "CONTACT";
public contactDAO () {
try {
Class.forName ( "com.pointbase.jdbc.jdbcUniversalDriver" );
} catch (Exception e) {
System.out.println("ERROR: failed to load pointbase JDBC
driver.");
e.printStackTrace();
return;
}
}
public static void main(String[] args) {
try {
Class.forName ( "com.pointbase.jdbc.jdbcUniversalDriver" );
} catch (Exception e) {
System.out.println("ERROR: failed to load pointbase JDBC
driver.");
e.printStackTrace();
return;
}
try {
Connection c =
DriverManager.getConnection(DBCONNECTION,USERNAME,PASSWORD);
Statement query = c.createStatement();
ResultSet result = query.executeQuery(SELECTQUERY);
System.out.println("------- Content of the Contact
Table: ------------\n");
while(result.next()) {
System.out.println("\t" + result.getString("NAME") +
"\t" + result.getString("STREET") +
"\t" + result.getString("CITY"));
}
}
catch(SQLException e) {
System.err.println("SQLException caught!");
e.printStackTrace();
}
}
public ResultSet getContact (String name) {
ResultSet rs = null;
try {
Connection c =
DriverManager.getConnection(DBCONNECTION,USERNAME,PASSWORD);
PreparedStatement ps = c.prepareStatement(SELECTNAME);
ps.setString(1,name);
rs = ps.executeQuery();
} catch(SQLException e) {
System.err.println("SQLException caught!");
e.printStackTrace();
}
return rs;
}
public int setContact (String name, String street, String city, String
state, int zipcode, String phone) {
int rowcount = 0;
try {
Connection c =
DriverManager.getConnection(DBCONNECTION,USERNAME,PASSWORD);
PreparedStatement ps = c.prepareStatement(INSERTNAME);
ps.setString(1,name); ps.setString(2,street);
ps.setString(3,city); ps.setString(4,state);
ps.setInt(5,zipcode); ps.setString(6,phone);rowcount
= ps.executeUpdate();
} catch(SQLException e) {
System.err.println("SQLException caught!");
e.printStackTrace();
}
return rowcount;
}
}
-----------------------------------------------------------------
and now the test script...
import java.sql.*;
public class daoTest {
public static void main(String[] args) {
int rowcount = 0;
ResultSet rs = null;
try {
contactDAO cd = new contactDAO();
cd.setContact("RALPH","123 My
Street","Reston","Virginia",20194,"(666)666-6666");
cd.getContact("RALPH");
/* while(rs.next()) {
System.out.println("\t" + rs.getString("NAME") +
"\t" + rs.getString("STREET") +
"\t" + rs.getString("CITY"));
}*/
} catch (Exception e) {
System.out.println("ERROR: failed to load pointbase JDBC
driver.");
e.printStackTrace();
return;
}
}
}