aspose file tools*
The moose likes JDBC and the fly likes Help with my homework please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Help with my homework please" Watch "Help with my homework please" New topic
Author

Help with my homework please

Sam Battery
Greenhorn

Joined: Oct 05, 2000
Posts: 10
This is my method to add data to an Access database. It won't make the connection though. The driver is set up correctly in my system so it must be my code. I'm sure it's something simple but I'm just not seeing it. Any help would be most appreciated.
public void createRecord(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection SESdb = DriverManager.getConnection("jdbcdbc:Records","","") ;
Statement dbCreate = SESdb.createStatement();
dbCreate.executeUpdate
("INSERT INTO STUDENTRECORDS" +
"VALUES (SSN.getText(), firstName.getText(), lastName.getText(), address.getText(),city.getText(),state.getText(),zip.getText(),phone.getText(),email.getText(),major.getText())");
dbCreate.executeUpdate
("INSERT INTO CREDITS"+
"VALUES (ssnField.getText(),course.getText(),section.getText(),grade.getText())");
}catch (Exception e){
System.out.println("Error Creating Record");
}
AJ Dussault
Ranch Hand

Joined: May 17, 2000
Posts: 53
In your getConnection statement, it looks like you are missing the username and password....I don't think they both can be null?
[This message has been edited by AJ Dussault (edited October 05, 2000).]
Monty Ireland
Ranch Hand

Joined: Oct 03, 2000
Posts: 161
There are plently of code examples within this form.
Search for Monty6.
Look at the JDBC DEMO code that comes with the JDK1.3.
Hope this helps...

------------------
We learn more from our mistake's than from our success's.


Multi Platform Database Developer & DBA on E.S.T.
Sam Battery
Greenhorn

Joined: Oct 05, 2000
Posts: 10
I don't have a login and password for Access. Is there one I should be using?
Monty Ireland
Ranch Hand

Joined: Oct 03, 2000
Posts: 161
I assuming you are using a windows platform.
Use ODBC to define you DataSource name.
Use ODBC to define which ms-access database you are trying use.
Within ODBC you should be able to find out UID & PWD for the DB.
If What I am saying confusses you.
Find a friend who knows ODBC.
Search the web for ODBC tutorials... there ar a few good ones.
If you are still stuck...
Come on back.. we are all here to help one and other.


------------------
We learn more from our mistake's than from our success's.
*** one of the jdk1.3 demo pgm's for java/jdbc/odbc uses a ms-access db with UID & PWD...
[This message has been edited by Monty Ireland (edited October 10, 2000).]
[This message has been edited by Monty Ireland (edited October 10, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi,
try not giving username and password .just say getConnection("jdbc dbc:records"); or getConnection("jdbc dbc:records"," "," ");
i think i did my application like that.let me check that and get back to you tomorrow.
hope it works
bye,
sridevi

[This message has been edited by Thomas Paul (edited October 31, 2000).]
Prasad Charasala
Ranch Hand

Joined: Nov 02, 2000
Posts: 67
Hi,
You don't need to supply userid and password if you did not set them at database creation time. Just for testing purpose I have created datasource. These are the steps to create datasource in MSAccess in WIN platforms.
1. Click Start-->Settings-->ControlPanel
2. DoubleClick on DataSources(ODBC)icon (something like this)
3. Click Add button
4. Select MicrosoftAccessDriver among the drivers and click finish.
5. Now in the new window give the DataSourceName, note down this this becomes part of your jdbc url later (I given this value as "Records").
6. description is optional. click create.
7. Select the folder, where do you want to store your DB and click ok.
8. Now you have created the datasource still it does not have any tables within it.
(Note:- If you want you can set up useid and password to the datasource clicking "Advanced..." button, where you supplied data source name)
Now you have all the setup to play with it with JDBC code.
See the example code below which creates a table first and then inserts two records in it and then retieve them and display. I have checked this and works fine.
<code>
import java.sql.*;
public class JdbcTest {

public void doTest(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc dbc:Records","","") ;
Statement stmt = con.createStatement();
System.out.println("Table Created?"+stmt.executeUpdate("create table mytable (sno integer, name varchar(20) )"));
System.out.println("Inserted?"+stmt.executeUpdate("insert into mytable values (1, 'Prasad')"));
System.out.println("Inserted?"+stmt.executeUpdate("insert into mytable values (2, 'Charasala')"));
ResultSet rs = stmt.executeQuery("select * from mytable");
while ( rs.next() ){
System.out.println("SNO ==>"+rs.getInt("sno")+"\n");
System.out.println("SNO ==>"+rs.getString("name")+"\n");
}
}catch (Exception e){
System.out.println("Error Creating Record");
e.printStackTrace();
}
}
public static void main( String[] args ) {
JdbcTest test = new JdbcTest();
test.doTest();
}
}
</code>

------------------
Prasad
[This message has been edited by Prasad Charasala (edited November 03, 2000).]


<B>Prasad</B>
Siva Jagadeesan
Ranch Hand

Joined: Oct 31, 2000
Posts: 160
Hi
I think Prasad code is correct it should work .
I guess u have problems with ur Datasource , which Prasad as given u step by step instruction . I don' see any mistake in ur code . It is perfectly alright to keep ur username and password null . So don't worry abt it.
Infact a suggestion , to test Whether ur connection is correct.
Create a small DataBase usin Access.
Then create a DataSource name as Prasad said.
try Reading from that Database and display it So u can be sure it works .
Thank you
siva


Rgds<p>--Siva Jagadeesan<br /><a href="http://java2simple.blog-city.com" target="_blank" rel="nofollow">http://java2simple.blog-city.com</a><br />Sun Certified Java2 Programmer<br />Sun Certified Web Component Developer<br />BEA Certified Weblogic Server 7 Enterprise Developer
yangelq yu
Greenhorn

Joined: Oct 03, 2000
Posts: 8
hi, I'm just learning jdbc, and I follow Parsad's instruction and codes for testing. I create a db in Access and compile the above code well in jdk1.2.2, but I got a run time exception - "No suitable driver". Should I have to download jdbc drivers and setup? or it comes with jdk1.2? pls clarify me. thanks.
Prasad Charasala
Ranch Hand

Joined: Nov 02, 2000
Posts: 67
Yu,
You don't need to download any JDBC drivers. JdbcOdbc Driver comes along with standard JDK package.

------------------
Prasad
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with my homework please