java.sql.SQLSyntaxErrorException: Unknown column 'indexno11' in 'where clause' - java

tu.java
//jdbc connection
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/st","root","root");
//query
String query = "select indexno,lname,fname from reg where indexno= ? and tel=?";
PreparedStatement ps = con.prepareStatement(query);
ps.setInt(1, Integer.parseInt(in.getText()));
ps.setInt(2, Integer.parseInt(tl.getText()));
ResultSet rs = ps.executeQuery();
if(rs.next()) {
String name11 =rs.getString("lname");
String name1 =rs.getString("fname");
int indexno11 =rs.getInt("indexno");
//passing value to wt frame
wt wt = new wt(name11, name1, indexno11);
wt.show();
}
rs.close();
con.close();`
wt.java
loging page index number used to check next frame credentials
public void actionPerformed(ActionEvent e) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/st","root","root");
String query = "select age from reg where indexno=indexno11";
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
String age11 =rs.getString("age");
System.out.println(age11);
}
rs.close();
con.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

You forgot your literalising single quotes around the value in the String query line.
String query = "select age from reg where indexno='indexno11'";

Related

get column values into an array

I have a database table with columns : name, residence, contact. and I want to retrieve only the values in the 'contact column' into an array. the following code is what I wrote:
List l = new ArrayList();
try {
String sql = "select * from members";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while (rs.next()) {
String con = rs.getString("Contact");
l.add(con);
System.out.println(l);
}
} catch (Exception e) {
e.getMessage();
}
but this generates the following result:
[0547615632]
[0547615632, 0246687643]
[0547615632, 0246687643, 0558581764]
whilst I have only 3 records in the table and each having a contact value.
please how do I write the code so that I can get only a single array displaying the result like this:
[0547615632, 0246687643, 0558581764]
thank you very much.
You should move the print command out of the while loop:
List l = new ArrayList();
try {
String sql = "select * from members";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while (rs.next()) {
String con = rs.getString("Contact");
l.add(con);
}
} catch (Exception e) {
e.getMessage();
}
System.out.println(l);
OR only print the current element in each iteration;
List l = new ArrayList();
try {
String sql = "select * from members";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while (rs.next()) {
String con = rs.getString("Contact");
l.add(con);
System.out.print(con + " ");
}
} catch (Exception e) {
e.getMessage();
}

MySQLSyntaxErrorException in JAVA

Somebody please help me.I am doing things right but i am getting an error.It is a JAVA application linked to MYSQL wamp server.
ERROR:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'Chege' at line 1
MY CODE:
public class MyQuery {
public Connection getConnection() {
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:mysql://"
+ "localhost:3306/employee_certificate", "root", "");
} catch (SQLException ex) {
Logger.getLogger(Query.class.getName())
.log(Level.SEVERE, null, ex);
}
return con;
}
public ArrayList<Item> getData(String EmpName) {
ArrayList<Item> list = new ArrayList<Item>();
Connection con = getConnection();
Statement st;
ResultSet rs;
try {
st = con.createStatement();
rs = st.executeQuery("SELECT Emp_Id, Emp_Name, Department "
+ "FROM staff WHERE Emp_Name = " + EmpName + " ");
Item I;
while (rs.next()) {
I = new Item(
rs.getString("Emp_Id"),
rs.getString("Emp_Name"),
rs.getString("Department"));
list.add(I);
}
} catch (SQLException ex) {
Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
}
return list;
}
}
Your query string is not correct. Should be something like the following:
rs=st.executeQuery("SELECT Emp_Id, Emp_Name, Department "
+ "FROM staff WHERE Emp_Name = '"+EmpName+"'");
But I'd recommend to use a PreparedStatement object for sending SQL statements to the database.
String query = "SELECT Emp_Id, Emp_Name, Department FROM staff WHERE Emp_Name = ?";
PreparedStatement preStatement = conn.prepareStatement(query);
preStatement.setString(1, EmpName);
ResultSet result = preStatement.executeQuery();
This approach is safer and more convenient.
You have a little problem in your query:
try {
st = con.createStatement();
//Add quotes 'YourString'
rs = st.executeQuery("SELECT Emp_Id, Emp_Name, Department "
+ "FROM staff WHERE Emp_Name = '" + EmpName + "' ");
Item I;
while (rs.next()) {
I = new Item(rs.getString("Emp_Id"), rs.getString("Emp_Name"), rs.getString("Department"));
list.add(I);
}
} catch (SQLException ex) {
Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
}
or for a safety Query use prepared statement:
try {
PreparedStatement ps = connection.prepareStatement("SELECT Emp_Id, Emp_Name, Department FROM staff WHERE Emp_Name = ?");
ps.setString(1, EmpName);
rs = ps.executeUpdate();
Item I;
while (rs.next()) {
I = new Item(rs.getString("Emp_Id"), rs.getString("Emp_Name"), rs.getString("Department"));
list.add(I);
}
} catch (SQLException ex) {
Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
}

Data not displayed in jList method from database

I am Using the following code :
DefaultListModel dModel = (DefaultListModel)jList1.getModel();
txtBilln.enableInputMethods(false);
try{
Class.forName("com.mysql.jdbc.Drvier");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/jns","root","jns");
Statement stmt = null ;
ResultSet rs = null ;
String SQL = "SELECT * FROM ELEB";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
while (rs.next()){
String bno= rs.getString("billn");
String cnamme = rs.getString("cname");
dModel.addElement(bno + "-" + cnamme);
}
jList1.setModel(dModel);
con.close();
}catch(Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
e.printStackTrace();
}

How to store the value of resultset as an int?

I need to open a div tag from jsp if the value of the resultset is more than 30000. But how to store the value of rs as an int?
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
con = DriverManager.getConnection (urlPrefix, "uname", "pwd");
System.out.println("got connection");
stmt = con.createStatement();
String strQuery = "SELECT count(*) FROM tablename where condition stmt";
rs = stmt.executeQuery(strQuery);
if (rs>30000) {
request.getParameter("view");
}
System.out.println("executed query");
} catch(Exception e) {
System.out.println(e);
}
rs = stmt.executeQuery(strQuery);
stmt.executeQuery return a ResultSet object and from the object we can retrive the result of
the query like below for your case.
rs = stmt.executeQuery(strQuery);
int count=0;
if(rs.next()){
count=rs.getInt(1);
}

Can we use two queries using prepared statement in one method

Can we use two queries in one method while using prepared statement, I have tried using this but invalid column name exception is coming.
My code snippets is as follows.
public double getPayroll(){
ResultSet rs = null;
ResultSet rs2 = null;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getDBConnection();
int employeeId;
String q1 = "select e_salary,e_house_rent,e_conv_allow,e_id
from employee";
pstmt = conn.prepareStatement(q1);
rs = pstmt.executeQuery();
double dailyPay=0,basicPay=0,payroll2=0;
int houseRent=0,convAllow=0;
while (rs.next()) {
dailyPay = rs.getInt(1)*.03;
houseRent=rs.getInt(2);
convAllow=rs.getInt(3);
employeeId=rs.getInt(4);
}
String q2="select att_status from attendance where
e_id=employeeId";
pstmt = conn.prepareStatement(q2);
rs2 = pstmt.executeQuery();
int noOfPresents = 0;
while(rs2.next()){
noOfPresents+=1;
}
basicPay=dailyPay*noOfPresents;
payroll2+=basicPay+houseRent+convAllow;
return payroll2;
} catch (Exception e) {
e.printStackTrace();
return 0.0;
} finally {
try {
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Your problem is that the sql in q2 assumes that there is a column named employeeId, but I suspect you want to insert the value of the variable employeeId.
Change it to
select att_status from attendance where e_id=?
Then execute
pstmt.setString(1, employeeId);
before executing pstmt.executeQuery();

Categories

Resources