Results 1 to 7 of 7
  1. #1
    shaheen is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Question connectivity in java using MSSQL server2005

    what are the steps for java connectivity using Microsoft SQL Server 2005 ?.i m using netbean. OS-windows7

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

    Default Re: connectivity in java using MSSQL server2005

    Moved from Learn Java > Java Example > Eclipse

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

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: connectivity in java using MSSQL server2005

    Either Google up the examples on MSDN for JDBC/SQL Server, and also go through the tutorial at Oracle for JDBC.
    MSDN will point you to the correct driver to use and what connection string to use as well as any setup needed for SQL Server itself.
    Please do not ask for code as refusal often offends.

  4. #4
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: connectivity in java using MSSQL server2005

    To get you started, here's some sample code. I made it with MS Access in mind, however you just need to swap out the jdbc driver "Class.forName(""); " part.

    Java Code:
    import java.sql.*;
    
    public class SQLConnAccessDb {
    	static Connection con = null;
    	public static void connDb() {
    		String dataSourceName = "DATABASE_NAME";
    		String dbURL = "jdbc:odbc:" + dataSourceName;
    		try {
    			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    			con = DriverManager.getConnection(dbURL, "","");
    		}
    		catch (Exception err) {
    			System.err.println("Error: " + err);
    		}
    	}
    }
    and here's some sample code for running queries and returning data (using this as the connection object)

    Java Code:
    mport java.sql.*;
    
    public class SQLQueryAccessDb {
    	static ResultSet rs = null;
    	static Statement s = null;
    	static SQLConnAccessDb conDb = null;
    	
    	public static String sqlQuery(String queryString, int returnColumn) {
    		rs = null;
    		String returnSku = null;
    		try {
    			s = SQLConnAccessDb.con.createStatement();
    			s.execute(queryString);
    			rs = s.getResultSet();
    			if(rs.next()) {
    				returnSku = rs.getString(returnColumn);
    			}
    			else {
    				returnSku = "xxx666xxx";
    			}
    		}
    		catch (Exception err) {
    			System.err.println("Error: " + err);
    			err.printStackTrace();
    		}
    		finally {
    			try {
    				s.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    		return returnSku;
    		
    	}
    	public static void closeDB() {
    		try {
    			s.close();
    		} catch (SQLException e1) {
    			e1.printStackTrace();
    		}
    		try {
    			SQLConnAccessDb.con.close();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    	}
    }

    so now you need to just modify this code, and come up with a way to call the connection block, then the query block, then the close connection block... and your set. I suggest calling them from whatever main program logic class you are building. -- of course how to do this is all over the net... so it shouldn't be too hard ;-P

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: connectivity in java using MSSQL server2005

    Well, you don't just need to swap the driver.
    You also need to change the connection URL, and possibly some other things on the SQL Server side (I seem to remember something to do with the port settings).
    Please do not ask for code as refusal often offends.

  6. #6
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: connectivity in java using MSSQL server2005

    if you use ODBC (makes things easy) then its quite similar to the code above. change the name of your ODBC connection. then your set.

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

    Default Re: connectivity in java using MSSQL server2005

    Do not use the JDBC/ODBC bridge.
    It's not intended as anything other than a demo of how a really basic JDBC driver works.
    It is seriously out of date as well.

    Any sane database provider has its own driver that is the one that should be used.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Replies: 1
    Last Post: 12-08-2011, 06:07 PM
  2. Replies: 0
    Last Post: 05-10-2011, 07:30 AM
  3. java connection to mssql
    By gerard kowara in forum JDBC
    Replies: 9
    Last Post: 10-14-2010, 09:07 AM
  4. getinng data from mssql server 2008
    By fcciko in forum JDBC
    Replies: 3
    Last Post: 04-21-2010, 01:59 PM
  5. Not able to delete MSSql database table entries
    By wickedrahul9 in forum Advanced Java
    Replies: 3
    Last Post: 10-17-2008, 12:14 PM

Posting Permissions

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