|
Home > Archive > SQL Server JDBC > March 2006 > jdbc connection
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| lukhenry@gmail.com 2006-02-28, 8:24 pm |
| Hi,
I am a beginner on JDBC with SQL Server 2005. I tried to write a
simple program to connect to the SQL Server 2005 that I installed on my
machine (localhost). Here is the program:
import java.sql.*;
public class JdbcTest {
public static void main(String[] args) {
try {
DriverManager.registerDriver(new
com.microsoft.sqlserver.jdbc.SQLServerDriver());
Connection connection =
DriverManager.getConnection("jdbc:sqlserver://localhost:1433",
"myusername", "mypasswd");
if (null == connection)
{
System.out.println("Connection is null...");
}
} catch(SQLException se) {
System.out.println("ERROR: " + se.getMessage());
}
}
}
However, I got the following error:
ERROR: The TCP/IP connection to the host has failed.
java.net.ConnectException: Connection refused: connect
Should I use localhost as the server name in this case? Since my SQL
server is installed on my local machine. Also, what is an instance
name? How can I find out the instance name?
| |
| Joe Weinstein 2006-02-28, 8:24 pm |
|
lukhenry@gmail.com wrote:
> Hi,
>
> I am a beginner on JDBC with SQL Server 2005. I tried to write a
> simple program to connect to the SQL Server 2005 that I installed on my
> machine (localhost). Here is the program:
>
> import java.sql.*;
>
> public class JdbcTest {
> public static void main(String[] args) {
> try {
> DriverManager.registerDriver(new
> com.microsoft.sqlserver.jdbc.SQLServerDriver());
> Connection connection =
> DriverManager.getConnection("jdbc:sqlserver://localhost:1433",
> "myusername", "mypasswd");
>
> if (null == connection)
> {
> System.out.println("Connection is null...");
>
> }
> } catch(SQLException se) {
> System.out.println("ERROR: " + se.getMessage());
> }
> }
> }
>
> However, I got the following error:
>
> ERROR: The TCP/IP connection to the host has failed.
> java.net.ConnectException: Connection refused: connect
Hi. Your program is fine. You just need to check the setup
of your DBMS, to seeif it is configured to accept connections
in 'mixed mode', ie: allowing the simple TCP socket connections
the driver makes.
HTH,
Joe Weinstein at BEA Systems
>
> Should I use localhost as the server name in this case? Since my SQL
> server is installed on my local machine. Also, what is an instance
> name? How can I find out the instance name?
>
| |
| coollll... 2006-02-28, 8:24 pm |
| Thanks Joe. It turns out that the TCP connection is not enabled in my
SQL server 2005 Express. I guess the default setting is that TCP
connection is not enabled. And somehow the port number is set to
dynamic port - meaning that the # is not 1433 in this case.
For those of you who has no JDBC + SQL Server experience before, here
are some tips on how to check your port#.
1. Go to "SQL Server Configuration Manager".
2. On the left pane, go to "SQL Server 2005 Network
Configuration"->"Protocols for (yourinstancename)".
3. Then double-click on the TCP/IP option, choose the "IP Address" tab,
and then check out your port #.
Here is my sample Java program which works with SQL Server 2005
Express:
import java.sql.*;
public class JdbcTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
DriverManager.registerDriver(new
com.microsoft.sqlserver.jdbc.SQLServerDriver());
Connection connection =
DriverManager.getConnection("jdbc:sqlserver:// yourservername\\your
instancename:1689;us
er=yourusername;pass
word=yourpasswd");
if (null != connection)
{
DatabaseMetaData meta = connection.getMetaData();
System.out.println("Driver name: " + meta.getDriverName());
System.out.println("Driver version: " + meta. getDriverVersion());
Statement stmt = connection.createStatement();
ResultSet rs= stmt.executeQuery("SELECT * FROM dbo.EMPLOYEES");
while (rs.next()) {
String mykeyfield= rs.getString(1);
String mydatafield= rs.getString(2);
System.out.println("key " + mykeyfield);
System.out.println("data " + mydatafield);
}
}
connection.close();
} catch(SQLException se) {
System.out.println("ERROR: " + se.getMessage());
}
}
}
| |
| jose Lopez 2006-03-14, 8:23 pm |
|
I'm having the same problem , if you get a solution please share it wit
me. Thanks
*** Sent via Developersdex http://www.droptable.com ***
|
|
|
|
|