Results 1 to 9 of 9
  1. #1
    moony is offline Member
    Join Date
    May 2012
    Posts
    23
    Rep Power
    0

    Default Northwind database connecting via Java

    Hi all!
    I have in my computer SQL Server Sample Databases (Northwind pubs).
    I'm going to connect to Northwind database via Java. How I can get the url of this database to connect to it?
    As I think I must start with jdbc:sqlserver: .... Isn't it?


    Thanks in advance!!

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Northwind database connecting via Java

    I'd recommend having a look at the documentation associated with your driver, which should describe how to formulate the URL. A quick google search turned up this webpage with an example: http://vietunicode.sourceforge.net/h...qlserver.htmlv

    Edit: moved to http://www.java-forums.org/jdbc/

  3. #3
    moony is offline Member
    Join Date
    May 2012
    Posts
    23
    Rep Power
    0

    Default Re: Northwind database connecting via Java

    Hi. I'm putting the code of my program which I've tried:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.Statement;

    public class Main {
    public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();
    // st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();

    int numberOfColumns = rsMetaData.getColumnCount();
    System.out.println("resultSet MetaData column Count=" + numberOfColumns);

    st.close();
    conn.close();
    }

    private static Connection getConnection() throws Exception {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String url = "jdbc:odbc:northwind";
    Class.forName(driver);
    return DriverManager.getConnection(url);
    }
    }

    The following errors are displayed:

    Exception in thread "main" java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknow n Source)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at Main.getConnection(Main.java:31)
    at Main.main(Main.java:9)

    What do you suggest to do?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,446
    Rep Power
    18

    Default Re: Northwind database connecting via Java

    Use a driver built for SQL Server, of which Microsoft has one or there's always the JTDS driver.
    The JDBC/ODBC bridge is not a good one to use for any number of reasons, and it also requires (as the message says) a DSN to be registered (using it that way).
    Please do not ask for code as refusal often offends.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Northwind database connecting via Java

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    SNK111 is offline Member
    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Default Re: Northwind database connecting via Java

    Quote Originally Posted by moony View Post
    Hi all!
    I have in my computer SQL Server Sample Databases (Northwind pubs).
    I'm going to connect to Northwind database via Java. How I can get the url of this database to connect to it?
    As I think I must start with jdbc:sqlserver: .... Isn't it?


    Thanks in advance!!
    To connect to MSSQL Server from a Java application, you need to use the JDBC API. The JDBC API provides classes and methods that connect to the database, load the appropriate driver, send SQL queries, retrieve results etc.

    HOW TO CONNECT TO THE DATABASE
    A ‘Connection’ object represents a connection with a database. To establish the connection, use the method ‘DriverManager.getConnection’. This method takes a string containing a URL which represents the database we are trying to connect to. Below is the sample code for establishing a connection:

    private String DATABASE_URL = "jdbc:odbc:embedded_sql_app"; // establish connection to database
    Connection connection = DriverManager.getConnection( DATABASE_URL,"sa","123" );

    Detailed discussion about the Database URL and how to create it can be found in the resource provided at the end of this post.

    QUERYING THE DATABASE
    The JDBC API provides three interfaces for sending SQL statements to the database, and corresponding methods in the ‘Connection’ interface create instances of them.
    1. Statement - created by the ‘Connection.createStatement’ methods. A ‘Statement’ object is used for sending SQL statements with no parameters.
    2. PreparedStatement - created by the ‘Connection.prepareStatement methods’. A ‘PreparedStatement’ object is used for precompiled SQL statements. These can take one or more parameters as input arguments (IN parameters).
    3. CallableStatement - created by the ‘Connection.prepareCall’ methods. ‘CallableStatement’ objects are used to execute SQL stored procedures from Java database applications.

    RETRIEVING THE RESULT
    A ‘ResultSet ‘is a Java object that contains the results of executing a SQL query. The data stored in a ‘ResultSet’ object is retrieved through a set of get methods that allows access to the various columns of the current row. The ‘ResultSet.next’ method is used to move to the next row of the ‘ResultSet’, making it the current row. The following code fragment executes a query that returns a collection of rows, with column ‘a’ as an ‘int’, column ‘b’ as a ‘String’, and column ‘c’ as a ‘float’:

    java.sql.Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
    while (rs.next()) { // retrieve and print the values for the current row
    int i = rs.getInt("a");
    String s = rs.getString("b");
    float f = rs.getFloat("c");
    System.out.println("ROW = " + i + " " + s + " " + f); }

    This is just a brief introduction on how to interact with a database from Java. For more details on the items discussed above as well as information on passing parameters, executing stored procedures etc. please refer to the following resource: ( Shahrar Nour Khondokar - Research into embedding SQL in C# and Java ) Here, you will also find information on how to interact with a database programmatically; i.e. without using SQL. Hope you find this useful.

    Source(s):
    http://www.shahriarnk.com/Shahriar-N...ng_SQL_in_Java

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,446
    Rep Power
    18

    Default Re: Northwind database connecting via Java

    Which the OP already knows (as shown by their code posted above), and is no use to them since the jdbc/odbc bridge driver is not the one they should be using to connect to SQL Server in the first place!

    So why, exactly, do you think that's at all useful in this thread?
    Please do not ask for code as refusal often offends.

  8. #8
    moony is offline Member
    Join Date
    May 2012
    Posts
    23
    Rep Power
    0

    Default Re: Northwind database connecting via Java

    Hi all !!
    Thanks for your replies and attention. SNK111 your information is very helpful for me.
    I really can't realize connection to Northwind... My problem is that I don't understand how to create the url...

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,446
    Rep Power
    18

    Default Re: Northwind database connecting via Java

    Have you got a proper SQL Server driver yet?
    If so, then wherever it is you got it from will also have documentation explaining what URL is required by their driver.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Connecting Java to my database
    By scooby009 in forum New To Java
    Replies: 0
    Last Post: 02-02-2012, 06:18 PM
  2. Connecting with database in java
    By UJJAL DHAR in forum New To Java
    Replies: 3
    Last Post: 06-05-2010, 07:34 AM
  3. Java connecting to sql database
    By markious in forum New To Java
    Replies: 3
    Last Post: 04-14-2010, 09:58 PM
  4. Connecting to Teradata Database from java
    By drkpradeep in forum Advanced Java
    Replies: 5
    Last Post: 02-18-2010, 09:16 AM
  5. Problem connecting to my Database using Java
    By javaneed in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 09:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •