java resultSet not updatable - java

i have a problem in resultset when i made update on value inside database and i have primarykey inside my db
this is the exception that i have
com.mysql.jdbc.NotUpdatable: Result Set not updatable
protected void processPairWords()
{
int count1=0;
try {
Statement st1;
Statement st2;
st1 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
st2 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet res1 = st1
.executeQuery("SELECT txt,freq,prob FROM searchtb WHERE txttype=1");
ResultSet res2= st2
.executeQuery("SELECT txt,freq,prob FROM searchtb WHERE txttype=2");
res1.beforeFirst();
res2.beforeFirst();
while (res1.next()){
while(res2.next())
{
if(res2.getString("txt").startsWith(res1.getString("txt")))
{
int prob2=res2.getInt("freq");
int prob1=res1.getInt("freq");
double prob=prob2/prob1;
res2.updateDouble("prob", prob);
res2.updateRow();
count1++;
System.out.println(res2.getString("txt"));
} }
System.out.println("loop1");
}
conn.commit();
System.out.println("pairs count"+count1 );
} catch (SQLException e) {
e.printStackTrace();
}
}

I think the table should have a primary key and the SQL query should select it.
Hope this can help you.

Related

MySQL checking if something is already in a column

I am new to MySQL and don't really know how to check if in the column "friendCode" the same value already exist and if that's the case it should look if in the same row in the "watched" column the value "0" is. I have already managed to check whether the code exists in a row. I just don't know how to look for the second condition.
Here is a picture how the table looks like:
And here that what I already managed to do:
public static boolean checkRow(String query) {
boolean ret = false;
try (Connection con = DriverManager.getConnection(url, user, password)) {
Statement stmt = con.createStatement();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs.next() && !rs.getString("watched").equals(0)) {
ret = false;
} else {
ret = true;
}
rs.close();
stmt.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return ret;
}
I think you are not written any select query in this program. If you are calling from the main method just check and try the below steps.
In Given Table watched column have integer DataType data.
Your Compare to String type.
Try it my code it's working if you have any concern revert back to this comment.
public static boolean checkRow(String query) {
boolean ret = false;
try (Connection con = DriverManager.getConnection(url, user, password)) {
Statement stmt = con.createStatement();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs.next() && rs.getString("watched")!=0) {
ret = false;
} else {
ret = true;
}
rs.close();
stmt.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return ret;
}

Get max value sql in Java

*I don't get value of max in function other. Value return is "0". I trying but not success :(
Image
public int PriceMax(int manhom){
Connection conn = this.connect();
int max = 0;
if(conn != null){
try {
java.sql.Statement statement = conn.createStatement();
String sql = "SELECT AVG(GiaSP) from tbsanpham where manhom = '"+manhom+"'";
ResultSet rs = statement.executeQuery(sql);
max = rs.getInt(sql);
} catch (SQLException ex) {
Logger.getLogger(CSDL.class.getName()).log(Level.SEVERE, null, ex);
}
}
return max;
}
Help!!!
int manhom = cbbNhomSanPham.getSelectedIndex();
CSDL csdl = new CSDL();
int max = csdl.PriceMax(manhom);
JOptionPane.showMessageDialog(null, "Nhóm sản phẩm: '"+cbbNhomSanPham.getName()+"' \nPrice max: '"+max+"' ");
You're not using it as it should be.
First of all, you use AVG but want MAX so change it to MAX(GiaSP). Second, you must use rs.next() to have your cursor go to the first row and then get information from it.
java.sql.Statement statement = conn.createStatement();
String sql = "SELECT MAX(GiaSP) from tbsanpham where manhom = '"+manhom+"'";
ResultSet rs = statement.executeQuery(sql);
if (rs.next()) {
max = rs.getInt(1);
}
ResultSet rs = stmt.executeQuery("select MAX(GiaSP) as maxGiaSP from tbsanpham where manhom = '"+manhom+"'");
if (rs.next())
{
int w = rs.getInt("maxGiaSP ");
// just return this int
}

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);
}

how to check database table is empty or not

i have a database with a table 'admin'.But presently just creating table not having any values.table columns are user_name and password.My motive is to check whether the table is empty or not.I am using mysql database.I tried following code and its fails.Please help me.
public void nullCheck() {
PreparedStatement stmt = null;
ResultSet rs = null;
String qry = "SELECT * From admin ";
try {
stmt = (PreparedStatement) conn.prepareStatement(qry);
rs = stmt.executeQuery();
boolean empty = true;
while( rs.next() ) {
// ResultSet processing here
empty = false;
}
if( empty ) {
Util.showWarningMessageDialog("null");
}
} catch (SQLException ex) {
Logger.getLogger(RemoveFaculty.class.getName()).log(Level.SEVERE, null, ex);
}
}
If you just need to check the table then you should use query:
String qry = "SELECT count(*) From admin ";
for better performance.
And get the row count from ResultSet to check the table is null or not.
int count=0;
while( rs.next() )
{
count=rs.getInt("count");
}
Try this code, I think this will do it. I updated it coz the first I post here is not working at all, my bad.
public void nullCheck() {
PreparedStatement stmt = null;
ResultSet rs = null;
String qry = "SELECT * From admin ";
try {
stmt = (PreparedStatement) conn.prepareStatement(qry);
rs = stmt.executeQuery();
int count = 0;
while(rs.next()){
count++;
}
if(count == 0){ // if equal to 0 then the table is null
bla bla bla
}
} catch (SQLException ex) {
Logger.getLogger(RemoveFaculty.class.getName()).log(Level.SEVERE, null, ex);
}
}

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