I want to set up a connection to a Microsoft SQL Server 2005 express on my PC.

try
        {

       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        Connection con = DriverManager.getConnection( "jdbc:mysql://localhost/db1","xxxx","yyyy");

        Statement stmt = con.createStatement();
          ResultSet rs = stmt.executeQuery("SELECT nume FROM tabel1");
        while (rs.next()) {

	String s = rs.getString("name");
        textArea.append(s);

        }
        }
        // Catches any error conditions
        catch (SQLException e)
        {
             System.out.println("connection DEAD");
        }

                catch (java.lang.ClassNotFoundException x)
        {
             System.out.println("class not found");
        }

About the SQL server, I can only connect to it from the Management Studio via Windows authentication. "PCNAME\Administrator" user and no password. I've tried adding logins but no use.

What to do ?

What error did you get? Or which exception was thrown?

Connection dead.

That's your own message. Print the stack trace instead.

I would also note: you are using the worst possible driver to try to connect to it. Don't use the jdbc-odbc bridge when there is an actual jdbc driver available.

If you do have to use the jdbc-odbc bridge driver, don't use a MySql connection string. Look up the proper connection string for the ODBC source.

Thanks Ezzaral.

LE: I did what I got off here: http://www.linglom.com/2007/03/04/accessing-sql-server-on-netbeans-using-jdbc-part-i-create-a-connection/

Again, my code is this:

public class NewClass {


    public static void main (String[] args){
       Connection conn = null;

    try {
        conn = DriverManager.getConnection("jdbc:mysql://localhost/database1?" +"user=xxx&password=yyy");
        // Do something with the Connection

    } catch (SQLException ex) {

        // handle any errors

    System.out.println("SQLException: " + ex.getMessage());

    System.out.println("SQLState: " + ex.getSQLState());
    System.out.println("VendorError: " + ex.getErrorCode());
}

    }



}

And the output is this:

run:
SQLException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
SQLState: 08S01
VendorError: 0

The code you posted above is not the same as the code in the tutorial you linked.

Your connection string is still wrong. Why are you trying to specify a "mysql" connection with the SQL Server driver? You also dropped the Class.forName() call.

try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;" +
            "databaseName=database1;user=sa;password=xxx;");
        // Do something with the Connection
        System.out.println("Success");

    } catch (SQLException ex) {

        // handle any errors

    System.out.println("SQLException: " + ex.getMessage());

    System.out.println("SQLState: " + ex.getSQLState());
    System.out.println("VendorError: " + ex.getErrorCode());
}
       catch (Exception e){
       System.out.println("Error");
       e.printStackTrace();
       
       }

run:
SQLException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "connect timed out. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
SQLState: 08S01
VendorError: 0

I set up remote TCP/IP connections from the Surface Area Configuration utility and rebooted.

Finally:

try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;" +
            "databaseName=database1;user=sa;password=xxx;");
        // Do something with the Connection
        System.out.println("Success");


    } catch (SQLException ex) {

        // handle any errors

    System.out.println("SQLException: " + ex.getMessage());

    System.out.println("SQLState: " + ex.getSQLState());
    System.out.println("VendorError: " + ex.getErrorCode());
}
       catch (Exception e){
       System.out.println("Error");
       e.printStackTrace();
       
       }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.