0

I'm a newbie in programming (Java) and I'm trying to figure out how to work with databases, using Microsoft SQL Server 2012.

I got the code below from a book, with changes only to the local server address, password, database name, etc.

The code runs indefinitely and only prints "The driver has been loaded!"

Also, it seems to make no difference what password or user name I write. I've tried many different address formats that I got from Microsoft and forums. Can anyone please tell me what I'm doing wrong here? Thanks!

package CoisaIdiota;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TesteDB {

    static String ConnectURL = "jdbc:sqlserver://localhost:1433;databaseName=teste";
    static String user = "Adm-PC\\Adm";
    static String pw = "password";
    static Connection conn;

    public static void main(String[] args) {
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
    } catch (Exception e) {
        System.err.println("The driver couldn’t be loaded!");
        System.err.println(e);
        e.printStackTrace();
        System.exit(-1);
    }
    System.out.println("The driver has been loaded!");

    try {
        conn = DriverManager.getConnection(ConnectURL, user, pw);
        Statement stmt = conn.createStatement();
        String query = "select ID from nomes";
        ResultSet rs = stmt.executeQuery(query);
        while(rs.next()){
        System.out.println(rs.getString("ID"));
        }
        rs.close();
        stmt.close();
    } catch (SQLException e){
        System.err.println("No connection possible.");
        e.printStackTrace();
        System.err.println("SQLException: " + e.getMessage());
        System.err.println("SQLState: " + e.getSQLState());
        System.err.println("VendorError: " + e.getErrorCode());
        System.exit(-1);
    }


    }
}
6
  • 1
    Define "runs indefinitely". Is it stuck in a loop (I only see one that wouldn't cause an infinite loop)? Commented May 5, 2013 at 13:52
  • can you connect to the db over the command line? I mean is the server running and the port correct? also, user and pw? Commented May 5, 2013 at 14:02
  • do you have appropriate libraries in your class path Commented May 5, 2013 at 14:15
  • User and password are correct (as in Microsoft SQL Server), the classpath seems to be working (as it loads the driver) and with "runs indefinitely" I mean that I have to click on "terminate" on Eclipse, otherwise it never ends. Commented May 5, 2013 at 15:49
  • When you say 'runs indefinitely', how long have you waited before terminating the application? I've experienced timeouts of the order of 30 seconds to 1 minute with SQL Server and JDBC (admittedly with SQL Server 2008, not 2012). If I remember correctly, I was getting timeouts because SQL Server hadn't been setup to enable TCP connections. See stackoverflow.com/a/8346110 for how to do this. Commented May 5, 2013 at 15:59

2 Answers 2

0

it seems to make no difference what password or user name I write.

This is because you print before the connection ,so "The driver has been loaded!" is just output:

try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
    } catch (Exception e) {
       ....
    }
    System.out.println("The driver has been loaded!");////you print here before the connection

try {
   ....
    }

to fix it , put your print after connection Like that:

 try {
     conn = DriverManager.getConnection(ConnectURL, user, pw);
     System.out.println("The driver has been loaded!");/////if it printed that's mean you enter the correct username and correct password , if not it will print the exception 
     ....
}
Sign up to request clarification or add additional context in comments.

Comments

0

try changing

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();

to

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

as explained here

but probably that does not solve it yet... i am looking :)

These are the things that i can see that could be the cause:

  • the server is not running
  • the port is wrong
  • the driver speaks over socket and the server listens via port (for that try to connect to 127.0.0.1 instead of localhost)
  • user/pw are incorrect

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.