I have connected an access db with java applet. In it I am giving values of students and their schools when working with schools, there are two schools (supposingly):
Little Flower School
St. Joseph's School
I have a method store and getchestname (it is for an inter-school contest. go the chest name as an unique id). Both the functions run sql commands as follows:
//getchestname :
ResultSet rs = db.exec("SELECT * FROM Pariticipants WHERE `school` = '"+schools[sc][1]+"' AND event = '"+events[ev][1]+"' AND category = '"+cat[ca][1]+"' AND gender = '"+g2+"'");
//Store:
rs = db.exec("INSERT INTO Pariticipants ( school , name, event, category , gender ,chestname ) VALUES ('"+schools[sc][1]+"','"+name+"','"+events[ev][1]+"','"+cat[ca][1]+"','"+g+"','"+cname+"')");
When the school is set to Little Flower School, it works perfectly but when I am working with the later one, it gives this error :
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression '`school` = 'St. Joseph\'s school' AND event = '50 m Race' AND category = 'Primary' AND gender = 'Boy''.
I think it is due to apostrophe (').
Edit: DBConn.java(db connection . earlier)
import java.sql.*;
/**
* Write a description of class DBConnector here.
*
* @author Darshan Baid
* @version (a version number or a date)
*/
public class DBConn
{
String accessFileName = "C:\\Users\\Darshan\\Documents\\Database1.accdb";
Connection con;
public ResultSet exec(String query)
{
try{
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt.execute(query); // execute query in table student
ResultSet rs = stmt.getResultSet(); // get any Result that came from our query
return rs;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
public void connect() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String connURL="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+this.accessFileName+";";
this.con = DriverManager.getConnection(connURL, "","");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public boolean close()
{
try{
con.close();
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
}
Now:
import java.sql.*;
public class DBConn
{
String accessFileName = "C:\\Users\\Darshan\\Documents\\Database1.accdb";
Connection con;
public ResultSet exec(String query)
{
try{
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt.execute(query); // execute query in table student
ResultSet rs = stmt.getResultSet(); // get any Result that came from our query
return rs;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
public ResultSet exec(String query,String[] ar)
{
try{
PreparedStatement userRecord_stmt = con.prepareStatement(query);
for(int i = 0; i< ar.length;i++)
userRecord_stmt.setString(1,ar[i]);
ResultSet userRecord_rs = userRecord_stmt.executeQuery();
return userRecord_rs;
}catch(Exception e)
{
e.printStackTrace();
return null;
}
}
public void connect() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String connURL="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+this.accessFileName+";";
this.con = DriverManager.getConnection(connURL, "","");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public boolean close()
{
try{
con.close();
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
}
And changes to chestname func. :
String ar[] = {schools[sc][1],events[ev][1],cat[ca][1],g2};
//ResultSet rs = db.exec("SELECT * FROM Pariticipants WHERE `school` = '"+schools[sc][1]+"' AND event = '"+events[ev][1]+"' AND category = '"+cat[ca][1]+"' AND gender = '"+g2+"'");
ResultSet rs = db.exec("SELECT * FROM Pariticipants WHERE `school` = ? AND `event` = ? AND `category` = ? AND `gender` = ?",ar);
error faced:
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]COUNT field incorrect