Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

DataBase Connection

  • 29-03-2009 4:09pm
    #1
    Registered Users, Registered Users 2 Posts: 86 ✭✭


    Hi guys. I have been trying to connect to microsoft sql server through java, but have been encountering a number of problems I cannot find the solution to. Perhaps someone could help. I jave installed the necessary JDBC driver and set the classpath appropriatly.
    The folloeing is the code I have been using to try to connect:

    /*
    * Main.java
    *
    * Created on November 27, 2008, 3:56 PM
    *
    */

    package sqlserversample;

    import java.sql.*;
    import com.microsoft.sqlserver.jdbc.*;

    /**
    *
    * @author Timothy M. Rodriguez
    */

    public class Main {

    /* jdbc driver name and database url
    * ms sql uses port 1433
    * replace "mysqlserver" with the ip and host of your database
    * if connecting from a *nix box, don't forget to use the FQDN and not just the NetBIOS host
    * if not, the ip is a safe bet
    */
    private static final String DATABASE_URL = "jdbc:sqlserver://mysqlserver:1433";
    private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLserverdriver";

    //used to connect to the database
    private static Connection connection = null;
    //used to execute queries on the database
    private static Statement statement = null;
    //this object is used to store your result sets
    private static ResultSet resultSet = null;
    //this object is used to stored data about the table
    private static ResultSetMetaData metaData = null;

    /** Creates a new instance of Main */
    public Main() {
    //default constructor
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {

    try {
    // load the driver class

    //this should work, but doesn't, not sure why, let me know in the comments if you do
    // Class.forName(DRIVER);
    // instead i created it this way
    SQLServerDriver sqlServerDriver = new SQLServerDriver();
    /*
    * I manually created a SQLServerDriver instance, this is due to
    * complications with the classpath establish connection to db
    */
    DriverManager.registerDriver(sqlServerDriver);
    connection = DriverManager.getConnection(DATABASE_URL,
    "user", "password");

    // create the statement for querying
    statement = connection.createStatement();

    //put your query string in the executeQuery method argument
    resultSet = statement.executeQuery("SELECT TOP 10 * FROM Table");
    metaData = resultSet.getMetaData();
    //I didn't use the metadata object, but showed you how to create it
    //the metaData object can be used to get column names and indices, column types, etc.

    while(resultSet.next()) {
    //the get object method is useful if you don't want to bother with the column type
    System.out.println(resultSet.getObject(3).toString());
    }

    } catch (SQLException sqlException) {
    //couldn't connect to the db or execute a query
    sqlException.printStackTrace();
    /* in a real webapp, you don't want to be printing stack traces
    * instead print a nice error page that doesn't include internal information
    * For example, "oops, it blew up"
    */
    }
    }
    }

    and the following are the errors I keep been thrown back:
    Main.java:11: package com.microsoft.sqlserver.jdbc does not exist
    import com.microsoft.sqlserver.jdbc.*;

    Main.java:54: cannot find symbol
    symbol : class SQLServerDriver
    location: class sqlserversample.Main
    SQLServerDriver sqlServerDriver = new SQLServerDriver();
    ^

    Main.java:54: cannot find symbol
    symbol : class SQLServerDriver
    location: class sqlserversample.Main
    SQLServerDriver sqlServerDriver = new SQLServerDriver();

    please if anyone can help, I would appreciate it.


Comments

  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    Classpath issue more than likely - what CP are you setting - give us an example please


  • Registered Users, Registered Users 2 Posts: 86 ✭✭NerfNerder


    Hey I actually got it working. Cheers mate.


Advertisement