File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding a record to a database-new to JDBC

 
Jo Eagle
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone Please help me - I am able to read data from a MS Access database and display it in my GUI - w/first and next buttons - how do I add-- take input from a user and write this information back to the database? Then be able to look it up. (save it) I'm trying to make an address book.
Any help would be apprecatied.
Jo
----Code for getting data----
public void FillInDataFields(){
boolean bMoreData;
bMoreData = false;
try {
bMoreData = this.rs.next();
if (bMoreData == true){
txtLastName.setText(rs.getString("LastName"));
txtFirstName.setText(rs.getString("FirstName"));
txtEmail.setText(rs.getString("Email"));
txtStreetAddress.setText (rs.getString"StreetAddress"));
txtCity.setText(rs.getString("City"));
txtState.setText(rs.getString("State"));
txtZip.setText(rs.getString("Zip"));
txtHPhone.setText(rs.getString("HPhone"));
txtWPhone.setText(rs.getString("WPhone"));
txtCell.setText(rs.getString("Cell"));
txtFax.setText(rs.getString("Fax"));
txtEmployer.setText(rs.getString("Employer"));
txtMisc.setText(rs.getString("Misc"));
}
} catch(Exception e) {
}
-----------------------------
 
Jo Eagle
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the JDBC class that goes with the previous submitted question.
Books - online resources - any help welcome. JO
import java.sql.*;
public class JDBC {
public static Connection con;
public static Statement stmt;
public static String SQL_STRING;
public static ResultSet my_rs;
public ResultSet getData(){
try {
//step 1 --load the driver
Class.forName("com.ms.jdbc.odbc.JdbcOdbcDriver");
//step 2 --create a connection
String url = "jdbc dbc:AddressBook";
con = DriverManager.getConnection(url,"","");
//step 3 --
stmt = con.createStatement();
//execute statement and return results
SQL_STRING = "SELECT * FROM AddressBook";
my_rs = stmt.executeQuery(SQL_STRING);
return my_rs;
} catch(Exception e) {
return my_rs;
}
}
}
 
rani bedi
Ranch Hand
Posts: 358
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of you SELECT query you need to write INSERT query.
you can refer the following URL for help. http://java.sun.com/docs/books/tutorial/jdbc/jdbc2dot0/sample.html
 
Jo Eagle
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this correct - make a separate class?
import java.sql.*;
public class InsertRows{
public static Connection con;
public static Statement stmt;
public static String SQL_STRING;
public static ResultSet my_rs;
public ResultSet InsertRows(){
try {
Class.forName("com.ms.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc dbc:AddressBook";
con = DriverManager.getConnection(url,"","");
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
Resultset uprs = stmt.executeQuery("SELECT* FROM AddressBook";
uprs.moveToInsertRow();
uprs.updateString("LastName","");
uprs.updateString("FirstName","");
uprs.updateString("StreetAddress","");
uprs.updateString("City","");
uprs.updateString("State","");
uprs.updateString("Zip","");
uprs.insertRow();
uprs.beforeFirst();
while(uprs.next()){
String name = uprs.getString("LastName");
.....
}
uprs.close();
stmt.close();
con.close();
}catch(SQLException ex){
......
}
}

[This message has been edited by Jo Eagle (edited March 30, 2001).]
[This message has been edited by Jo Eagle (edited March 30, 2001).]
 
Peter Tran
Bartender
Posts: 783
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jo,
Just make sure the driver you're using supports the JDBC 2.0 interface. It looks like you're using the JDBC-ODBC type I driver. I'm not sure if any type I driver supports the JDBC 2.0 interface. You can try, and it may give you an error like "not supported functionality." A better approach is to use a java.sql.Statement and code a SQL insert statement.
-Peter
[This message has been edited by Peter Tran (edited March 30, 2001).]
 
Andrew Shafer
Ranch Hand
Posts: 338
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That will work if your driver is JDBC 2.0 compliant
Usually, you need to use INSERT to make a new row in your table or you can use UPDATE to change data in a row. (or on some DBMS you can use SELECT . . . INTO)
I suggest you read a little SQL tutorial. (www.google.com search for: SQL tutorial)
If you have a better grasp of the underlying technology, the java will be simple.
You don't need to make a whole new class, just a new method should work dandy.


 
Jo Eagle
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank guys - I'll give your suggestions a try - starting first with sql tutorial. I'm sure I'll be back with more questions - but thank you for your time and help. Jo
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic