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: Problem with ODBC...

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with ODBC...

    This is my first Java ODBC application so I'm sure this is me, but this is geting aggrivating. I'm using NetBeans 7.3 and connecting a SQL Server Express 2012 Database.

    I have the following code which works:
    private static void processToday() {
            //ArrayList<ClsEmployee> empList = new ArrayList<ClsEmployee>();
            //int rowCount = 0; 
            ClsConn conn = new ClsConn();
            Connection con = conn.openConn();
            try {
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT Statement");
                //rowCount = rs.getMetaData().getColumnCount();
                ClsEmployee emp;
     
                while (rs.next()) {
                    emp = new ClsEmployee();
                    emp.setEmpId(rs.getInt("employeeId"));
                    emp.setRollDate(rs.getDate("vacationRollDate"));
                    emp.setLastRoll(rs.getInt("LastYearlyUpdate"));
     
                    System.out.print("Employee ID: " + emp.getEmpId());
                    System.out.print(" roll date: " + emp.getRollDate());
                    System.out.println(" last updated: " + emp.getLastRoll());
                    //empList.add(emp);
                }
                con.close();
            } catch (Exception e) {
                System.err.println("Exception: " + e.getMessage());
            }
        }

    However if I uncomment int rowCount = 0; I get the following error:
    Exception: [Microsoft][ODBC Driver Manager] Invalid string or buffer length

    I'm at a loss, thus the reason I'm here... thanks!

    Forgot to mention if I debug it, it runs fine only if I try to "run" it does it fail...

    This is my connection code:

    import java.sql.*;
     
    public class ClsConn {
        private Connection conn;
     
        public Connection openConn() {
            try {
                conn = DriverManager.getConnection("jdbc:odbc:IntranetSql");
                System.out.println("DSN Connection ok.");
            } catch (Exception e) {
                System.out.println("Exception: " + e.getMessage());
            }
            return conn;
        }
    }
    Last edited by temlehdrol; March 7th, 2013 at 06:54 AM. Reason: added more information


  2. #2
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with ODBC...

    I added:
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    in the connection class and still nothing...

    --- Update ---

    Ok we have some progress... changed the connection type to JTDS, this is the new connection code:
    public Connection openConn() {
            try {
                System.out.println("Trying the connection now...");
                //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Class.forName("net.sourceforge.jtds.jdbc.Driver");
                //conn = DriverManager.getConnection("jdbc:odbc:IntranetSql");
                conn = java.sql.DriverManager.getConnection("jdbc:jtds:sqlserver://localhost/Intranet;instance=sqlexpress","sa","passwordHere");
                System.out.println("DSN Connection ok.");
            } catch (Exception e) {
                System.out.println("DSN Exception: " + e.getMessage());
            }
            return conn;
        }

    I now get: DSN Exception: Network error IOException: Connection refused: connect

    --- Update ---

    Ok, yet another update and another error... but hey it's progress
    I enabled TCP/IP for SQL Server and opened up port 1433... I now get the following error:

    Exception: Cannot open database "Intranet" requested by the login. The login failed.

    I have also tried: jdbc:jtds:sqlserver://localhost/Intranet;instance=sqlexpress:1433 and I get the same error

    --- Update ---

    Ok for fun I just changed the Database name to Master and it now connects... the query fails because the table it's looking for doesn't exist in Master. This is just beyond awesome... any ideas?

    --- Update ---

    Even tried changing my sql statement to "USE Intranet SELECT..." still telling me the database doesn't exist...

    --- Update ---

    conn string is now:
    conn = java.sql.DriverManager.getConnection("jdbc:jtds:sq lserver://localhost:1433/master;user=sa;password=password");

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with ODBC...

    Ok, combo of hatred of M$ and Java here.... apparently when I installed Visual Studio Web edition or IIS it put a copy of SQL Server 2008 Express and Java was nice enough to grab that one vs the 2012 which Visual studio was... Stopped the service for 2008 and now it's working... I'm going to go grab a drink now.

Similar Threads

  1. Replies: 0
    Last Post: December 25th, 2011, 01:59 PM
  2. Read (using Odbc)and write using (POI api)
    By amruta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 9th, 2010, 10:12 AM
  3. Cannot locate sun.jdbc.odbc.jbcOdbc driver
    By birdlover2010 in forum JDBC & Databases
    Replies: 7
    Last Post: November 6th, 2010, 02:57 PM
  4. Project using JavaBeans, JSP & ODBC.
    By m.b.taylor in forum Paid Java Projects
    Replies: 0
    Last Post: July 15th, 2010, 09:36 AM