Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   problem in inserting record in ms access. (http://www.velocityreviews.com/forums/t953008-problem-in-inserting-record-in-ms-access.html)

Navnath Gadakh 10-04-2012 04:47 AM

problem in inserting record in ms access.
 

package javaapplication3;
import java.sql.*;

public class JavaApplication3 {
Connection con;
Statement st;
ResultSet rs;

public JavaApplication3()
{
connect();
}

public void connect()
{
try
{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);

String db = "jdbc:odbc:db1";
con = DriverManager.getConnection(db);
st = con.createStatement();
String sql = "select * from Table1";
rs = st.executeQuery(sql);

while(rs.next())
{
String fname = rs.getString("fname");
String lname = rs.getString("lname");
String address = rs.getString("address");
String email = rs.getString("email");
String mobile = rs.getString("mobile");

System.out.println(fname+lname+address+email+mobil e);
}

}catch(Exception ex)
{

}

try
{
rs.moveToInsertRow();
rs.updateString("fname","abc");
rs.updateString("lname","xyz");
rs.updateString("address","mubmai");
rs.updateString("email","abc@gmail.com");
rs.updateString("mobile","99854874154");
rs.insertRow();

st.close();
rs.close();

}
catch(Exception err)
{
System.out.println("Error!!!");
}


}
public static void main(String[] args) {
// TODO code application logic here
new JavaApplication3();
}
}

Arne Vajhj 10-04-2012 09:35 PM

Re: problem in inserting record in ms access.
 
On 10/4/2012 12:47 AM, Navnath Gadakh wrote:
> package javaapplication3;
> import java.sql.*;
>
> public class JavaApplication3 {
> Connection con;
> Statement st;
> ResultSet rs;


Make them private.

> public JavaApplication3()
> {
> connect();
> }
>
> public void connect()
> {
> try
> {
> String driver = "sun.jdbc.odbc.JdbcOdbcDriver";


I will not recommend the JDBC ODBC bridge unless you are absolutely
forced to use it.

There are plenty of alternatives.

> Class.forName(driver);
>
> String db = "jdbc:odbc:db1";
> con = DriverManager.getConnection(db);
> st = con.createStatement();


As Martin has explained then this is very likely the cause of
your specific problem.

> String sql = "select * from Table1";
> rs = st.executeQuery(sql);
>
> while(rs.next())
> {
> String fname = rs.getString("fname");
> String lname = rs.getString("lname");
> String address = rs.getString("address");
> String email = rs.getString("email");
> String mobile = rs.getString("mobile");
>
> System.out.println(fname+lname+address+email+mobil e);
> }
>
> }catch(Exception ex)
> {


Always print the exception.

> }
>
> try
> {
> rs.moveToInsertRow();
> rs.updateString("fname","abc");
> rs.updateString("lname","xyz");
> rs.updateString("address","mubmai");
> rs.updateString("email","abc@gmail.com");
> rs.updateString("mobile","99854874154");
> rs.insertRow();


I would suggest using plain INSERT instead of this.

> st.close();
> rs.close();
>
> }
> catch(Exception err)
> {
> System.out.println("Error!!!");


Always ...

> }
>
>
> }
> public static void main(String[] args) {
> // TODO code application logic here
> new JavaApplication3();


Don't do such heavy work in the constructor.

> }
> }


Arne




Roedy Green 10-05-2012 03:33 AM

Re: problem in inserting record in ms access.
 
On Wed, 3 Oct 2012 21:47:34 -0700 (PDT), Navnath Gadakh
<navnathgadakh@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>
>package javaapplication3;


It is traditional to:

1. ask a question
2. describe what you expected the code to do.
3. describe what the code actually did.
--
Roedy Green Canadian Mind Products http://mindprod.com
The iPhone 5 is a low end Rolex.



Lew 10-05-2012 05:48 PM

Re: problem in inserting record in ms access.
 
Roedy Green wrote:
> It is traditional to:
>
> 1. ask a question


For good advice in this area see
http://www.catb.org/esr/faqs/smart-questions.html

This is pretty much the canon of Usenet (and everywhere else) request protocol.

> 2. describe what you expected the code to do.
> 3. describe what the code actually did.


Roedy's excellent point about this tradition is rooted in pragmatics.

It is difficult for those motivated to help you if we have not enough data with which
to make an assessment.

Another good source of advice on how to ask code questions:

http://sscce.org/

Roedy's mindprod.com also covers this advice.

--
Lew

Arne Vajhøj 10-06-2012 12:27 AM

Re: problem in inserting record in ms access.
 
On 10/4/2012 7:24 PM, Martin Gregorie wrote:
> On Thu, 04 Oct 2012 17:35:39 -0400, Arne Vajhøj wrote:
>> On 10/4/2012 12:47 AM, Navnath Gadakh wrote:
>> I will not recommend the JDBC ODBC bridge unless you are absolutely
>> forced to use it.
>>

> The OP mentioned (in the title) that he is using MS Access. Is there a
> JDBC driver for it?


No.

But there are plenty of other database.

Going back I can see that I wrote something utterly incomplete.

When I said alternatives it was alternative databases not
JDBC drivers.

Egg on my face.

Arne


Arne Vajhøj 10-06-2012 01:08 PM

Re: problem in inserting record in ms access.
 
On 10/6/2012 7:36 AM, Martin Gregorie wrote:
> On Fri, 05 Oct 2012 20:27:11 -0400, Arne Vajhøj wrote:
>> But there are plenty of other database.
>>

> Indeed, unless the OP was told to use Access (but why, when Derby is
> freely available and has the advantage of being written in Java?).


And if one does not like JavaDB/Derby, then there is MySQL, PostgreSQL,
H2 and FireBird - plus the free editions of Oracle, DB2 and SQLServer.

> I've
> had to fix Access systems in the past and remain unimpressed with it.


The Jet database engine was never anything special. The special was the
integrated application building tool. But that does not help when
writing the app in Java anyway.

Arne





All times are GMT. The time now is 09:12 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.