Results 1 to 7 of 7
- 07-02-2012, 02:12 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 1
- Rep Power
- 0
- 07-03-2012, 03:42 AM #2
Re: connectivity in java using MSSQL server2005
Moved from Learn Java > Java Example > Eclipse
.gif)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 07-03-2012, 09:29 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,463
- Rep Power
- 16
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.
- 07-06-2012, 04:49 PM #4
Senior Member
- Join Date
- Apr 2012
- Posts
- 127
- Rep Power
- 0
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.
and here's some sample code for running queries and returning data (using this as the connection object)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); } } }
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
- 07-08-2012, 02:12 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,463
- Rep Power
- 16
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.
- 07-10-2012, 12:10 AM #6
Senior Member
- Join Date
- Apr 2012
- Posts
- 127
- Rep Power
- 0
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.
- 07-10-2012, 10:37 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,463
- Rep Power
- 16
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
-
Proposed use of Java to enhance asp.net/mssql architecture -- sanity check please
By MyTimeFinder in forum New To JavaReplies: 1Last Post: 12-08-2011, 06:07 PM -
Error while connecting to MSSQL with FIPS enabled in SSL
By harshalpop in forum JDBCReplies: 0Last Post: 05-10-2011, 07:30 AM -
java connection to mssql
By gerard kowara in forum JDBCReplies: 9Last Post: 10-14-2010, 09:07 AM -
getinng data from mssql server 2008
By fcciko in forum JDBCReplies: 3Last Post: 04-21-2010, 01:59 PM -
Not able to delete MSSql database table entries
By wickedrahul9 in forum Advanced JavaReplies: 3Last Post: 10-17-2008, 12:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks