problem in inserting record in ms access.

N

Navnath Gadakh

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:eek:dbc: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+mobile);
}

}catch(Exception ex)
{

}

try
{
rs.moveToInsertRow();
rs.updateString("fname","abc");
rs.updateString("lname","xyz");
rs.updateString("address","mubmai");
rs.updateString("email","(e-mail address removed)");
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();
}
}
 
A

Arne Vajhøj

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:eek:dbc: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+mobile);
}

}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","(e-mail address removed)");
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
 
R

Roedy Green

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.
 
L

Lew

Roedy said:
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.
 
A

Arne Vajhøj

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
 
A

Arne Vajhøj

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top