To make it connect to northwind, do the following:
- Go to http://sourceforge.net/projects/jtds/files/ and download jtds-1.2.4-dist.zip
- Unzip
jtds-1.2.4-dist.zip
and extract:
- jtds-1.2.4.jar into C:\Program
Files\Java\jdk1.6.0_16\jre\lib\ext ( or whichever version you are using
in BlueJ – Find from BlueJ about button)
- put ntlmauth.dll in C:\Program
Files\Java\jdk1.6.0_16\jre\bin
- In your code: (see testconnection2.java)
- import java.sql.*
- define the driver with the
command Class.forName("net.sourceforge.jtds.jdbc.Driver");
- this says we are using the jtds
driver, an open source driver.
- this requires a catch of Exception
e
- create a Connection object
using the DriverManager.getConnection method, passing it
"jdbc:jtds:sqlserver://localhost/Northwind;instance=sqlexpress"
- the connection object knows the
database.
- create a Statement object for that
connection with <your Connection object>.createStatement()
- I think you can have multiples,
but am not sure
- If you don't want a result row back,
tell your Statement object to execute a string which is an SQL
statement as <your statement object>.execute ("your sql
statement with no ;")
- If you want a result row back, tell
your Statement object to execute a string which is an SQL statement as
ResultSet <name a variable> = <your statement
object>.executeQuery("your sql statement")
- read it by telling your result set
to go to the next line with result.next();
- then result.getString("column
name");
- To manage 2 different database
connections, create a class with no variables and pass it the database
connection string. Let the class do all the connection and sql
statement execution.
- It doesn't need any instance
variables.
- see
sample
- Here is
an example with a menu. The loop to take in a menu option isn't
working quite right.
Great site: http://softwaresalariman.blogspot.com/2007/04/jdbc-to-sql-server-express.html
--- but we used windows authentication. The readme.sso from the
zip
file said to put ntlmauth.dll in C:\Program
Files\Java\jdk1.6.0_16\jre\bin
Got sample code here: http://www.java-tips.org/other-api-tips/jdbc/how-to-connect-microsoft-sql-server-using-jdbc.html