944,081 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 713
  • Java RSS
Oct 9th, 2009
0

NEED HELP!!!

Expand Post »
Hello everyone, can someone help me with this please?
Create class which will create tables in data base (Microsoft Access). About the tables: number of columns, type of data, lenght of the data.
And mantaining the information (the data) in the tables- add data, delete data and edit data.
So far I have codes which are connected with SQL, and I need them in Access. Here are some codes.
Java Syntax (Toggle Plain Text)
  1. import java.sql.*;
  2.  
  3. public class CreateTable{
  4. public static void main(String[] args) {
  5. System.out.println("Table Creation Example!");
  6. Connection con = null;
  7. String url = "jdbc:mysql://localhost:3306/";
  8. String dbName = "jdbctutorial";
  9. String driverName = "com.mysql.jdbc.Driver";
  10. String userName = "root";
  11. String password = "root";
  12. try{
  13. Class.forName(driverName).newInstance();
  14. con = DriverManager.getConnection(url+dbName, userName, password);
  15. try{
  16. Statement st = con.createStatement();
  17. String table = "CREATE TABLE Employee11(Emp_code integer, Emp_name varchar(10))";
  18. st.executeUpdate(table);
  19. System.out.println("Table creation process successfully!");
  20. }
  21. catch(SQLException s){
  22. System.out.println("Table all ready exists!");
  23. }
  24. con.close();
  25. }
  26. catch (Exception e){
  27. e.printStackTrace();
  28. }
  29. }
  30. }
I can post more codes but they're all with SQL.
Any help?
Reputation Points: 10
Solved Threads: 0
Junior Poster
didi00 is offline Offline
166 posts
since Apr 2008
Oct 9th, 2009
0
Re: NEED HELP!!!
Will this work?:
Java Syntax (Toggle Plain Text)
  1. import java.sql.*;
  2.  
  3. public class dbAccess
  4. {
  5. public static void main(String[] args)
  6. {
  7. try
  8. {
  9. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  10. String database =
  11. "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=myDB.mdb;";
  12. Connection conn = DriverManager.getConnection(database, "", "");
  13. Statement s = conn.createStatement();
  14.  
  15. // create a table
  16. String tableName = "myTable" + String.valueOf((int)(Math.random() * 1000.0));
  17. String createTable = "CREATE TABLE " + tableName +
  18. " (id Integer, name Text(32))";
  19. s.execute(createTable);
  20.  
  21. // enter value into table
  22. for(int i=0; i<25; i++)
  23. {
  24. String addRow = "INSERT INTO " + tableName + " VALUES ( " +
  25. String.valueOf((int) (Math.random() * 32767)) + ", 'Text Value " +
  26. String.valueOf(Math.random()) + "')";
  27. s.execute(addRow);
  28. }
  29.  
  30. // Fetch table
  31. String selTable = "SELECT * FROM " + tableName;
  32. s.execute(selTable);
  33. ResultSet rs = s.getResultSet();
  34. while((rs!=null) && (rs.next()))
  35. {
  36. System.out.println(rs.getString(1) + " : " + rs.getString(2));
  37. }
  38.  
  39. // drop the table
  40. String dropTable = "DROP TABLE " + tableName;
  41. s.execute(dropTable);
  42.  
  43. // close and cleanup
  44. s.close();
  45. conn.close();
  46. }
  47. catch(Exception ex)
  48. {
  49. ex.printStackTrace();
  50. }
  51. }
  52. }
Reputation Points: 10
Solved Threads: 0
Junior Poster
didi00 is offline Offline
166 posts
since Apr 2008
Oct 9th, 2009
1
Re: NEED HELP!!!
Click to Expand / Collapse  Quote originally posted by didi00 ...
Will this work?:
Try yourself.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 10th, 2009
0
Re: NEED HELP!!!
The question is this: I work in Eclipse. How to connect Eclipse with Access?
The code is just fine, it has no errors.
The errors that the code is giving me are:
Java Syntax (Toggle Plain Text)
  1. java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Could not find file '(unknown)'.
  2. at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
  3. at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
  4. at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
  5. at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
  6. at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
  7. at java.sql.DriverManager.getConnection(Unknown Source)
  8. at java.sql.DriverManager.getConnection(Unknown Source)
  9. at dbAccess.main(dbAccess.java:12)
Reputation Points: 10
Solved Threads: 0
Junior Poster
didi00 is offline Offline
166 posts
since Apr 2008
Oct 10th, 2009
0
Re: NEED HELP!!!
google search --> Database Access Eclipse tutorial
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 10th, 2009
0
Re: NEED HELP!!!
My result:
Java Syntax (Toggle Plain Text)
  1. run:
  2. java.sql.SQLException: [Microsoft][Sterownik ODBC Microsoft Access ] Nie mo?na znale?? pliku '(nieznane)'.//unknown
  3. at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
  4. at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
  5. at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3073)
  6. at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
  7. at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
  8. at java.sql.DriverManager.getConnection(DriverManager.java:582)
  9. at java.sql.DriverManager.getConnection(DriverManager.java:185)
  10. at didi00.dbAccess.main(dbAccess.java:23)
where is your "sun.jdbc.odbc.JdbcOdbcDriver" ?
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 10th, 2009
0
Re: NEED HELP!!!
sun.jdbc.odbc.JdbcOdbcDriver is ok.

I resolved this problem in my post with source code: same theme
http://www.daniweb.com/forums/thread182674.html
good luck
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 11th, 2009
0
Re: NEED HELP!!!
Thanks. One more question:
Should I create ODBC entry for the data base this way:
Open the Administrative Tools icon in your Control Panel.
Double-click on the Data Sources (ODBC) icon inside.
Choose the System DSN tab.
Click on Add in the System DSN tab.
Select the Microsoft Access Driver. Click Finish.
In the next screen, click Select to locate the database.
Give the database a Data Source Name (DSN).
Click OK.
I'm new in data bases so any help will work.
Thanks
Reputation Points: 10
Solved Threads: 0
Junior Poster
didi00 is offline Offline
166 posts
since Apr 2008
Oct 11th, 2009
0
Re: NEED HELP!!!
Click to Expand / Collapse  Quote originally posted by didi00 ...
Thanks. One more question:
Should I create ODBC entry for the data base this way:
Open the Administrative Tools icon in your Control Panel.
Double-click on the Data Sources (ODBC) icon inside.
Choose the System DSN tab.
Click on Add in the System DSN tab.
Select the Microsoft Access Driver. Click Finish.
In the next screen, click Select to locate the database.
Give the database a Data Source Name (DSN).
Click OK.
I'm new in data bases so any help will work.
Thanks
Did it work for you?
Reputation Points: 30
Solved Threads: 7
Junior Poster
Jocamps is offline Offline
120 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Hi friends can anybody tell what is the diff bw clone() and copy
Next Thread in Java Forum Timeline: writing to a file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC