开发者

Not able to perform update in MS ACCESS 2007 using prepared stmt using jdbc

开发者 https://www.devze.com 2023-02-20 12:08 出处:网络
I am writing a code to update a specific column in access db by executing a java code. Here is what i have done till now.

I am writing a code to update a specific column in access db by executing a java code. Here is what i have done till now.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.io.*;

public class Main 
{

  private static Connection getConnection() throws Exception{
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String url = "jdbc:odbc:repo3";
    String username = "";
    String password = "";
    Class.forName(driver);
    return DriverManager.getConnection(url, username, password);
  }


   public static void main(String[] args) throws Exception 
   {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    ResultSet rs = st.executeQuery("SELECT READABLEKEY,OBJECTID,LOCALEID,REFTRANSLATION FROM Dummy where TRANSLATION =''");
    int i=0;
   try {
         while(rs.next())
         {
            i++;
          String Readable_Key=rs.getString("READABLEKEY");
          String Object_Id=rs.getString("OBJECTID");
          int Locale_Id=rs.getInt("LOCALEID");
           //String RefTranslation=rs.getString("REFTRANSLATION");
           System.out.println("here");
         BufferedReader br = new BufferedReader(new InputStreamReader(rs.getAsciiStream("REFTRANSLATION")));
         String RefTranslation = br.readLine();
         br.close();


           System.out.println("locale id is"+Locale_Id);

             switch(Locale_Id)
             {
             case 12297:
             String UpdtTranslation="UPDATE Dummy set TRANSLATION='GER"+RefTranslation+"'WHERE LOCALEID="+Locale_Id+"";          
             System.out.println("update query is "+UpdtTranslation);
             st.executeUpdate(UpdtTranslation);

             int r=st.executeUpdate(UpdtTranslation);
             System.out.println(r+"rows updated successfully!");

             }

        }
        System.out.println("I is "+i);//to see no.of records.
    }
    catch(Exception e) 
    {
    e.printStackTrace();
    }    
        finally
        {
                    try
                    {
                    st.close();
                    conn.close();
                    }
                    catch(Exception e)
                    {

                    }
                    finally
                    {

                    }
        }

    }

}

I know we can make use of Prepared statement too. I even tried u开发者_JAVA百科sing that. But i don't know how to update all the rows using prepared stmt. I tried using for loop. But, what should be the condition? i mean do i have to specify the no.of rows myslef? or should i write a separate code for getting the counts? Please do let me know whether my above approach is correct or not. If NOT please tell me the looping in prepared stmt.


try UPDATE Dummy set TRANSLATION='GER' & TRANSLATION where READABLEKEY=12297 to update the existing value with a string

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号