Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Cannot connect Java program to database

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Cannot connect Java program to database

    I am working on my matric java programming project and I need to be able to save data to a Microsoft Access database 2000 format. The following code is the constructor method for my array manager object.

    import java.sql.*;
     
    public class DBManager
    {
     
        private Connection conn;
     
        public DBManager()
        {
            try
            {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String dbname = "database.mdb";
                conn = DriverManager.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + dbname);
                System.out.println("Connection successful");
     
                Statement s = conn.createStatement();
                String name = "erin";
                String query = "SELECT * FROM Table1 WHERE name = \"" + name + "\";";
                System.out.println(query);
                s.execute(query);
     
                ResultSet rs = s.getResultSet();
            }
            catch (Exception e)
            {
                System.out.println("ERROR: " + e);
                e.printStackTrace();
            }
        }
    }

    And I am getting the following error:
    ERROR: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    	at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
    	at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
    	at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072)
    	at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
    	at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
    	at java.sql.DriverManager.getConnection(DriverManager.java:579)
    	at java.sql.DriverManager.getConnection(DriverManager.java:243)
    	at DBManager.<init>(DBManager.java:15)
    	at NewClass.main(NewClass.java:13)

    This is my main method to run the above code:
    public class NewClass {
        public static void main(String[] args)
        {
            DBManager m = new DBManager() ;
        }
    }

    I am using NetBeans IDE 7.0.1 to program this. Could anyone help me identify the problem with this code, which according to the error message is this line:
    conn = DriverManager.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + dbname);

    If any more information is required, please ask.
    Thanks


  2. #2
    Junior Member psabbate's Avatar
    Join Date
    Aug 2012
    Posts
    20
    My Mood
    Amused
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: Cannot connect Java program to database

    Everyone wants to go to heaven ... but nobody wants to die

    Nissi Group, Servicios IT,
    Diseņo Web, Desarrollo de Software, SEO,
    LinkedIn

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot connect Java program to database

    Create the DSN from administrative tools in your control panel. Create a username and pasword,
    then try this:
    conn=DriverManager. getCOnnection(jdbc : odbc : DSN,user,pws);
    Last edited by Dios; August 29th, 2012 at 10:56 AM.

Similar Threads

  1. Cant connect with database
    By ronn1e in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 4th, 2011, 04:09 PM
  2. connect java to a database
    By ridg18 in forum JDBC & Databases
    Replies: 1
    Last Post: December 25th, 2010, 11:28 AM
  3. Replies: 6
    Last Post: August 18th, 2010, 05:41 PM
  4. Java Swing Tables ( JTable Models ) to connect to Database
    By javaprogrammer in forum JDBC & Databases
    Replies: 0
    Last Post: January 26th, 2010, 04:13 PM
  5. Replies: 0
    Last Post: January 26th, 2010, 04:10 PM

Tags for this Thread