1 Replies - 1943 Views - Last Post: 02 August 2013 - 12:38 AM Rate Topic: -----

#1 jmanelson   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-February 09

SQL Server 2012 and MS JDBC Help

Posted 01 August 2013 - 11:56 PM

Here is what I have, Errors posted below

I am Using KryoNet for my server

package com.johnathan.infinitearena.server;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Server;
import com.esotericsoftware.minlog.Log;
import com.johnathan.infinitearena.server.Packet.Packet0LoginRequest;
import com.johnathan.infinitearena.server.Packet.Packet1LoginAccepted;
import com.johnathan.infinitearena.server.Packet.Packet2Message;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
import java.sql.Driver;

public class ServerMain {
	private Server server;
	private Connection con;
	private Statement stmt;
	private ResultSet rs;

	public ServerMain() throws IOException{
		server = new Server();
		server.addListener(new NetworkListener());
		try {
			System.out.println("Trying to load driver for db");
			System.out.println("Loaded driver for db");
		} catch (ClassNotFoundException e2) {
			System.out.println("couldnt load driver for db");
		String dbserver = "localhost\\SQLEXPRESS";
		//String dbserver = "localhost";
		int port =1433;
		//String user = "Jman-PC\\Jman";
		String user = "Johnathan Elson";
		String password = "johnathan";
		String database = "User_Info";
		String connectionUrl = "jdbc:sqlserver://" +server+":"+port+";user="+user+";password="+password+
		try {
			System.out.println("Trying to connect");
			con = DriverManager.getConnection(connectionUrl);
		} catch (SQLException e1) {
			System.out.println("couldnt connect");
		try {
			Statement stmt = con.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
		try {
			rs = stmt.executeQuery("SELECT COUNT(*) FROM USERS");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
		try {
				try {
					System.out.println("User Count : "+rs.getInt(1));
				} catch (SQLException e) {
					// TODO Auto-generated catch block
		} catch (SQLException e) {
			// TODO Auto-generated catch block
	public static void main(String[] args){
			new ServerMain();
		} catch(IOException e){
	private void registerPackets(){
		Kryo kryo = server.getKryo();

00:00 INFO: [kryonet] Server opened.
Trying to load driver for db
Loaded driver for db
Trying to connect
couldnt connect
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host [email protected], port 1433 has failed. Error: "null. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2243)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.johnathan.infinitearena.server.ServerMain.<init>(ServerMain.java:60)
at com.johnathan.infinitearena.server.ServerMain.main(ServerMain.java:101)
Exception in thread "main" java.lang.NullPointerException
at com.johnathan.infinitearena.server.ServerMain.<init>(ServerMain.java:67)
at com.johnathan.infinitearena.server.ServerMain.main(ServerMain.java:101)

The I am not sure why it is not connecting and also I am not sure why I have to wrap everything in Try/Catches (related to the Database and the JDBC code). I followed a video tutorial fort he MS JDNC and there was no Try/Catches required for that person. I receive a ClassNotFoundException unless i use the Try/Catches.

Some additional info to try and help debug:

-The SQLEXPRESS server is running and i set its ports to 1433.

-If I take out all of this JDBC code the Kryonet Server is functional and CAN connect to clients and send and receive packets through tcp.

- I created a Login on the SQL 2012 server with that user and pass and created a User for the User_Info database.

-Just to clarify this is not homework and I am not sure if the issue I am having is a connection issue or some extension of the ClassNotFoundException. From searching the web I have seen this is a problem that affects many (novices like myself) and my solution was to wrap everything JDBC related in Try/Catches like eclipse advised me but it seems wrong.

-I have added the MS JDBC library as an External Jar in my project build path and my Windows CLASSPATH is

.;C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip;C:\Users\Jman\Documents\Java Libraries\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc4.jar

-The program gets to the line : con = DriverManager.getConnection(connectionUrl); and then throws an SQLException
resulting in the "couldnt connect" line.

I do not know where to go from here, any help is appreciated. I apologize for this huge post but I just wanted to give as much info as possible so thanks for those who do read through it. Part of me thinks its some type of connection issue and the other part thinks that there is something wrong with the project setup (eclipse). As I said earlier this kryonet server is functional but the JDBC code is the issue. This is the full ServerMain class posted.

Thanks again everyone

Is This A Good Question/Topic? 0
  • +

Replies To: SQL Server 2012 and MS JDBC Help

#2 jmanelson   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-February 09

Re: SQL Server 2012 and MS JDBC Help

Posted 02 August 2013 - 12:38 AM

Well Once again I get frustrated and am about to give up after quite some time and post here on D.I.C and then immediately see my very pathetic mistake. The problem is that I was using the "server" variable in the connectionURL when the one I wanted was dbserver. the server is my actual server object.

I apologize for this.

Please delete this thread.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1