I wrote program in that I need to update 10000. The query is:
for (Ticket ticket : existInBoth) {
if (!ticket.isUpdatable() && !ticket.isDeleted()) continue;
String query = String.format("update tickets SET vehicle_id = %d , price = %d , free = %d,time = '%s',date='%s',url='%s',deleted_at=NULL WHERE unique_key = '%s'",
ticket.getVehicle().getId(), ticket.getPrice(), ticket.getFree(), ticket.getTime(), ticket.getDate(), ticket.getUrl(), ticket.getUniqueKey());
if (db.update(query) > 0)
updatedDates.add(ticket.getDate());
}
and db.update
#Override
public int update(String sql) {
synchronized (Main.THREAD_GUARD) {
int result = -1;
Connection connection = open();
try {
Statement statement = connection.createStatement();
result = statement.executeUpdate(sql);
statement.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(connection);
}
return result;
}
}
It seems realy easy. But when I ran it I see this updating take about 300MB of memory.
I carefully close all statements and connections.
Related
I'm using JDBC to make a banking system with Java. The user should be able to type in a number in a text field, and deposit said amount of money into the bank account. I would like to then update the account balance in the Microsot Access database.
I currently have this:
try (Connection con = DriverManager.getConnection("jdbc:ucanaccess://C://Users//User//IdeaProjects//Database4.accdb")) {
Statement users = con.createStatement();
ResultSet sr = users.executeQuery("Select * from Registrations");
Boolean duplicate = false;
while (sr.next()) {
if (userID.equals(sr.getString(2))) {
match = sr;
duplicate = true;
System.out.println("Welcome " + match.getString(2));
System.out.println("Your balance is " + match.getString(3));
break;
}
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == depositButton) {
String depositString = depositField.getText();
int depositAmount = Integer.parseInt(depositString);
try {
System.out.println(match.getInt(3) + depositAmount);
match.updateInt("AccBalance", match.getInt(3) + depositAmount);
match.updateRow();
}
catch (SQLException ex) {
ex.printStackTrace();
}
When I try to do this, I get the error 'attempt to assign to non-updatable column'.
I'm very new to Java and tried looking online to find fixes for this issue but couldn't find anything useful.
Try the code below
try (Connection con = DriverManager.getConnection("jdbc:ucanaccess://C://Users//mghosh22//IdeaProjects//Database4.accdb")) {
// Query example UPDATE user SET balance = 10000 WHERE user.id = 1;
PreparedStatement updateBalance = conn.prepareStatement("UPDATE <table> SET balance = ? WHERE <wich row update>");
// For each query parameter, set a value to execute the same
// preparedStatement.setParameterType(countEach?StartingWithOne, yourQueryParameter);
updateBalance.setDouble(1, 10,000.00);
// Execute update return the amount of rows your query affected, how we are talking about account balance, the number of rows affected may be ever one
if (updateBalance.executeUpdate() == 1) {
System.out.println("Row updated");
}
}
I am doing a project in Java Swing and using SQLite as my database.
This is the function I wrote for deleting a record from the Room table in my database.
public void Delete() {
String room_code = jTextField5.getText();
String sql = "DELETE FROM Room WHERE room_code = '" + room_code + "'";
try {
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(null, "Room Deleted Successfully");
}
else {
JOptionPane.showMessageDialog(null, "Invalid Room Code");
}
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
However, I am met with the following Exception: SQLException: query does not return results.
I have tried to use pst.executeUpdate() as suggested in other answers but it says "int cannot be converted into resultset".
A DELETE statement does not return a result set. You should call method executeUpdate rather than method executeQuery.
Also, you can use place holders with a PreparedStatement.
Also you should use try-with-resources
Consider the following code.
public void Delete() {
String room_code = jTextField5.getText();
String sql = "DELETE FROM Room WHERE room_code = ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(room_code);
int count = ps.executeUpdate();
if (count > 0) {
JOptionPane.showMessageDialog(null, "Room Deleted Successfully");
}
else {
JOptionPane.showMessageDialog(null, "Invalid Room Code");
}
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
I'm trying to take two random rowid from my database. Everything works but I have a scenario when there is only one rowid. I want to make a loop on my try/catch until there is second number in my database.
What I'm doing wrong? Thank you
public void Kaslaimejo() {
String sql = "SELECT rowid FROM Zaidejai WHERE Pirmas < 4 ORDER BY random() LIMIT 2";
Integer value1 = null, value2 = null;
Integer judesiukas1 = null, judesiukas2 = null;
int a = 0;
int k = 15; // kiek kartu? Reikia infinity padaryti
for (a = 0; a < 3; a++) {
try {
Connection conn = Serveris.connect();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
if (rs.next()) {
value1 = rs.getInt("rowid");
if (rs.next()) {
value2 = rs.getInt("rowid");
PreparedStatement buvo = conn.prepareStatement("UPDATE Zaidejai SET Numeriukas = ? WHERE rowid = ?");
buvo.setInt(1, i);
buvo.setInt(2, value1);
int buvolala = buvo.executeUpdate ();
PreparedStatement buvo2 = conn.prepareStatement("UPDATE Zaidejai SET Numeriukas = ? WHERE rowid = ?");
buvo2.setInt(1, i);
buvo2.setInt(2, value2);
int buvolala2 = buvo2.executeUpdate ();//
i++;
}
System.out.println("Pirmas zaidejas" + value1); // atspausdina 1 random zaideja is duomenu bazes
System.out.println("Antras zaidejas" + value2); // atspausdina 2 random zaideja is duomenu bazes
}
} catch (SQLException e) {
a--;
//System.out.println(e.getMessage());
}
}
}
Right now my program loops two times and then gives me SQLException. How I can loop my program until there is no SQLException?
OK, I've tried to write what I think you're trying to do.
You wait for ever until someone puts at least two entries in the database.
You extract two values, process them, then wait some more.
Some points to watch out:
1. Object comparisons need to be made with .equals() not with ==
2. You might want to provide some way to break out of the infinite loop I've written (while(true)).
3. Careful with null values. They might produce NullPointerException.
4. Try to break up your code into methods. Each large block of code could go into each own method.
public void Kaslaimejo(){
String sql = "SELECT rowid FROM Zaidejai WHERE Pirmas < 4 ORDER BY random() LIMIT 2";
Integer judesiukas1 = null, judesiukas2 = null;
while(true) {
List<Integer> values = new ArrayList<>();
while (values.size() < 2) {
try (Connection conn = Serveris.connect();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
if( rs.next() ){
Integer value = rs.getInt("rowid");
values.add(value);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
try( Connection conn = Serveris.connect()) {
PreparedStatement buvo = conn.prepareStatement("UPDATE Zaidejai SET Numeriukas = ? WHERE rowid = ?");
buvo.setInt(1, i);
buvo.setInt(2, values.get(0));
int buvolala = buvo.executeUpdate ();
PreparedStatement buvo2 = conn.prepareStatement("UPDATE Zaidejai SET Numeriukas = ? WHERE rowid = ?");
buvo2.setInt(1, i);
buvo2.setInt(2, values.get(1));
int buvolala2 = buvo2.executeUpdate ();//
i++;
}catch (SQLException e) {
e.printStackTrace();
}
Connection conn = Serveris.connect();
try {
PreparedStatement pstmt = conn.prepareStatement("SELECT Pirmas FROM Zaidejai WHERE rowid = ?");
PreparedStatement pstmt2 = conn.prepareStatement("SELECT Pirmas FROM Zaidejai WHERE rowid = ?");
pstmt.setInt(1, values.get(0));
pstmt2.setInt(1, values.get(1));
ResultSet myrsv = pstmt.executeQuery();
ResultSet myrsv2 = pstmt2.executeQuery();
{
if (myrsv.next()) {
judesiukas1 = myrsv.getInt("Pirmas");
if (myrsv2.next()) {
judesiukas2 = myrsv2.getInt("Pirmas");
}
}
//System.out.println("Pirmo zaidejo veiksmas" + myrsv.getInt("Pirmas"));
//System.out.println("Antro zaidejo veiksmas" + myrsv2.getInt("Pirmas"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (judesiukas1.equals(judesiukas2)) // careful here. NullPointerException may happen.
{
try {
PreparedStatement laim = conn.prepareStatement("UPDATE Zaidejai SET Rezultatas = ? WHERE rowid = ?"); // ble ble update reikia naudoti , o ne insert into. Insert kai sukuriame nauja kazka tik
PreparedStatement laim2 = conn.prepareStatement("UPDATE Zaidejai SET Rezultatas = ? WHERE rowid = ?");
laim.setString(1, "Lygiosios");
laim.setInt(2, values.get(0));
laim2.setString(1, "Lygiosios");
laim2.setInt(2, values.get(1));
int irasyk = laim.executeUpdate (); // kodel executeupdate, o ne executequery????
int irasyk2 = laim2.executeUpdate (); // kodel executeupdate, o ne executequery????
{
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print("Lygiosios");
} else {
// (1) - Rock
// (2) Scissors
// (3) - Paper
switch (values.get(0)){
case 1:
if (judesiukas2 == 2)
System.out.print("Zaidejas 1 wins!");
else
System.out.print("Zaidejas 2 wins!");
break;
case 2:
if (judesiukas2 == 3)
System.out.print("Zaidejas 1 wins!");
else
System.out.print("Zaidejas 2 wins!");
break;
case 3:
if (judesiukas2 == 1)
System.out.print("Zaidejas 1 wins!");
else
System.out.print("Zaidejas 2 wins!");
break;
}
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The logic becomes easier if you add the values to a list
var values = new ArrayList<Integer>();
while (values.Count < 2) {
try (Connection conn = Serveris.connect();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql))
{
while (values.Count < 2 && rs.next()) {
Integer v = rs.getInt("rowid");
values.Add(v);
}
} catch (SQLException e) {
}
}
//TODO: process the values here
The advantage is, that you can retrieve one value at the first database query and the second at a later one or both in the same round and you don't have to keep track of which one of two variables to use.
(Bear with me with the syntax details, I'm not a Java programmer.)
How i can loop my program until there is no SQLException?
Change this (because, it will only allow to loop two times)
for (a = 0; a < 2; a++) {
to
while(true)
Put everything inside while(true), if exception occurred, then it will come out from the while loop. Something similar :
try
{
while(true)
{
...
...
}
...
}
catch(SQLException e)
{
// do somthing
}
I have code, where I have single quote or APOSTROPHE in my search
I have database which is having test table and in name column of value is "my'test"
When running
SELECT * from test WHERE name = 'my''test';
this works fine
If I use the same in a Java program I am not getting any error or any result
But If I give the name with only single quote then it works
SELECT * from test WHERE name = 'my'test';
Could you please help me out to understand.
Java code is
Connection con = null;
PreparedStatement prSt = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.
getConnection("jdbc:oracle:thin:#localhost:1521:orcl"
,"user","pwd");
String query = "SELECT * from "
+ "WHERE name = ? ";
prSt = con.prepareStatement(query);
String value = "my'mobile";
char content[] = new char[value.length()];
value.getChars(0, value.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i = 0; i < content.length; i++) {
if (content[i] == '\'')
{
result.append("\'");
result.append("\'");
}
else
{
result.append(content[i]);
}
}
prSt.setObject(1, result.toString());
int count = prSt.executeUpdate();
System.out.println("===============> "+count);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally{
try{
if(prSt != null) prSt.close();
if(con != null) con.close();
} catch(Exception ex){}
}
You don't have to escape anything for the parameter of a PreparedStatement
Just use:
prSt = con.prepareStatement(query);
prSt.setString("my'mobile");
Additionally: if you are using a SELECT statement to retrieve data, you need to use executeQuery() not executeUpdate()
ResultSet rs = prst.executeQuery();
while (rs.next())
{
// process the result here
}
You might want to go through the JDBC tutorial before you continue with your project: http://docs.oracle.com/javase/tutorial/jdbc/index.html
preparedbatch method takes resultset of size 1000.
I am inserting data using prepared statement and batch.
Using LIMIT I am pulling 1000 rows from mysql database, java.. and calculating some value from column values of each row..
public class Method {
void preparedBatch (ResultSet rs, PreparedStatement insert) throws Exception{
int y;
int arr[]= new int[40];
while (rs.next()){
for(y=2;y<=41;y++){
arr[y-2]=rs.getInt(y);
}
insert.setString(1, rs.getString(1));
insert.setLong(2, calculate(arr));
insert.addBatch();
}
{
insert.executeBatch();
insert.clearBatch(); // I did this to avoid oOM
}
rs.close(); //if I do not close, then I get OOM error
}
long calculate (int[] arr){
long sum =0;
for(int k=0;k<40;k++){
sum+= arr[k]*pow(2,k);
}
return sum;
}
long pow(long base, int exponent)
{
if (exponent == 0)
return 1; // base case;
double temp = pow(base, exponent/2);
if (exponent % 2 == 0)
return (long) (temp * temp);
else
return (long) (base * temp * temp);
}
void method(){
Method k = Insert.m1;
System.out.println("in metho");
Database d1= new Database(); // for reading from tables
d1.setPassword("abc");
d1.setUrl("jdbc:mysql://localhost/DB");
d1.setUser("root");
// object to calculate 2 raise to 39 stuff
ResultSet rs =null;// result set for 2500 lines selected
PreparedStatement ps=null,insert=null; //ps for selecting 1000 lines //insert for inserting into binary table
final Connection con;
try {
con = DriverManager.getConnection(d1.getUrl(), d1.getUser(), d1.getPassword());
con.setAutoCommit(false);
insert = con.prepareStatement("insert into binarydata values(?,?)");
int z;
for(z=0;z<=850000;z=z+1000)
{
ps = con.prepareStatement("select * from table1 LIMIT "+z +", 1000");
rs= ps.executeQuery();
try {
k.preparedBatch(rs,insert);//k is global object of Methodclass
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rs.close();
if(z%200000==0){
System.out.println(z +" inserted this time");
}
if(z==850000){
System.out.println(z +"mouse");
}
}
{
ps = con.prepareStatement("select * from table1 LIMIT 851000 , 364");
insert = con.prepareStatement("insert into binarydata values(?,?)");
rs= ps.executeQuery();
try {
k.preparedBatch(rs,insert);
System.out.println("finally inserting");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(rs.getString(1));
}
rs.close();
}
try {
con.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
con.close();
ps.close();
insert.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
My main class creates object of Method and invokes method from it,
my array is 0,1,0,1,1,1,1,1,,0,0,0,0,0,0,0,1,0 ( it is an example, all 851364 rows contain such data)
if my arr for example is 0,1,0, then I am calculating sum+= arr[index]*2 (power index).. like this..
I have no knowledge of Springs, otherwise I would have used that to handle data of my size.
Please help me understand, why in method preparedBatch, I have to close resultset?
Also, please suggest optimized code of this.
It took me some 9 minutes, to read 85136 lines and insert respective sum..