User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » How to connect SQL to Java? (wanna make $10?) Page [1]  
InsaneMan
All American
22802 Posts
user info
edit post

I'm building a website that uses an SQL database, but cant get Java to connect to SQL Server Express thats running on my local computer, or to an SQL server on an other website.
I've tried many combinations of the jdbc:sqlserver:// url, but they all "refuse" to connect, except when I give it a nonexistant URL then it gives a NullPointerException as it should.

How can I connect Java to SQL?



If someone will come to my apartment (less than 1/2 mile from campus, I can drive you), I'll pay you $10
to solve this problem.


For the lazier people, please post solutions here...



import java.sql.*;
import java.util.*;

public class TestSQL{
public static void main(String a[]){
System.out.println("Starting query...");
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Enumeration e = DriverManager.getDrivers();
Driver driver = null;
while(e.hasMoreElements()){
driver = (Driver) e.nextElement();
System.out.println("driver: "+driver+" jdbcCompliant="+driver.jdbcCompliant());
}
Properties prop = new Properties();
//prop.setProperty("user","b");
//prop.setProperty("password","nopass");
//Connection connection = driver.connect("jdbc:sqlserver://localhost;",prop);
Connection connection = driver.connect("jdbc:sqlserver://localhost:1433;",prop);
/*Statement stmt = connection.createStatement();
ResultSet createTable = stmt.executeQuery(
"create table tt (a varchar(5), b varchar(10), c number(4));"
);*/
//ResultSet allDrugs = c.query("select * from drug d"); //all drugs
//ResultSet allDrugs = c.query("select * from drug d"); //all drugs
connection.close();
}catch(Exception e){
e.printStackTrace();
}
}
}


OUTPUT:


Starting query...
driver: com.microsoft.sqlserver.jdbc.SQLServerDriver@1fb8ee3 jdbcCompliant=true
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the ho
st has failed. java.net.ConnectException: Connection refused: connect
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(U
nknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Sour
ce)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)
at TestSQL.main(TestSQL.java:21)

1/23/2006 2:00:50 AM

smoothcrim
Universal Magnetic!
18968 Posts
user info
edit post

no one is going to drive to your place and look at your code for $10.

1/23/2006 3:06:33 AM

Noen
All American
31346 Posts
user info
edit post

are you running both the java server and SQL db locally?

if not, try it. could be that your firewall/router is blocking the ports.

1/23/2006 3:37:53 AM

InsaneMan
All American
22802 Posts
user info
edit post

smoothcrim they dont need a car. I can drive them here and back. And if they know what they're doing, the whole thing would be done in 30 minutes.

Noen yes I have SQL Server Express running locally. I also tried on a website using the server data they gave me. Both give the same error, the one I quoted above.

1/23/2006 7:02:55 AM

philihp
All American
8349 Posts
user info
edit post


Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = java.sql.DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName="+databaseName+";selectMethod=cursor;","b","nopass")

1/23/2006 7:27:21 AM

InsaneMan
All American
22802 Posts
user info
edit post

Ignoring your reversal of jdbc and sqlserver, and removing the databaseName variable from the URL, I got a new error:

D:\PR\SQL\java and sql>java -classpath sqljdbc.jar;. TestSQL
Starting query...
driver: com.microsoft.sqlserver.jdbc.SQLServerDriver@1fb8ee3 jdbcCompliant=true
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at TestSQL.main(TestSQL.java:26)
D:\PR\SQL\java and sql>

I dont know the databaseName of my local SQL Express program, but earlier I tried the same thing with an internet server that I did know the databaseName of (without selectMethod=cursor) and got the error in my first post.

[Edited on January 23, 2006 at 7:41 AM. Reason : ]

1/23/2006 7:40:47 AM

Shaggy
All American
17820 Posts
user info
edit post

just for shits i was looking at my local MSDE configuration. It was installed as part of j# express 2005 and i never used it.

Looks like TCP/IP is disabled by default.

Dunno if that might be it or not. I R DB NEWB

1/23/2006 11:56:53 AM

zorthage
1+1=5
17148 Posts
user info
edit post

can you connect from a commandline interface with all the info?

try to eliminate the code, see if its a DB config issue

1/23/2006 11:19:28 PM

 Message Boards » Tech Talk » How to connect SQL to Java? (wanna make $10?) Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.39 - our disclaimer.