*
The moose likes JDBC and the fly likes Adding a record to a database-new to JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Adding a record to a database-new to JDBC" Watch "Adding a record to a database-new to JDBC" New topic
Author

Adding a record to a database-new to JDBC

Jo Eagle
Greenhorn

Joined: Feb 14, 2001
Posts: 13
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

Joined: Feb 14, 2001
Posts: 13
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

Joined: Feb 06, 2001
Posts: 358
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


Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Jo Eagle
Greenhorn

Joined: Feb 14, 2001
Posts: 13
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

Joined: Jan 02, 2001
Posts: 783
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

Joined: Jan 19, 2001
Posts: 338

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.



!_I_Know_Kung_Fu_!
Jo Eagle
Greenhorn

Joined: Feb 14, 2001
Posts: 13
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
 
Consider Paul's rocket mass heater.
 
subject: Adding a record to a database-new to JDBC