facebook

Can’t connect to SQL Server Database using Database Explorer

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #232565 Reply

    Mork
    Participant

    Hi,

    I’ve tried several connection strings to try to connect to a SQL Server Database from Database Explorer. None of them work.

    Here’s an example of my connection info in the connection profile in SQL Server database explorer.

    “jdbc:microsoft:sqlserver://localhost:1433
    user = “sa”
    password = “xxxxxxxxxx”

    I get an “error while trying to log into the database”.

    (this happens too with the connector/j driver with MySQL though the “MySQL driver works OK)

    I added the three SQL Server JAR files from the MS site to the project so that should not be the issue I’m having.

    I’ve also tried to include the database in the connection string like this:

    “jdbc:microsoft:sqlserver://localhost:1433;Database=HibernateDemo;User=sa;Password=xxxxxxxxxxxxxxx”

    But, this didn’t work either. I get the error when I actually try to connect to the database.

    I would appreciate any ideas what’s going on and what I should try next.

    Thanks.

    — M

    #232574 Reply

    Riyad Kalla
    Member

    Mork,
    Try and download the jTDS drivers from source forge. Add them as the JARs for your MS SQL Server, make sure the server isn’t firewalled, hen setup your connection string as you gave above:
    “jdbc:microsoft:sqlserver://localhost:1433;Database=HibernateDemo;User=sa;Password=xxxxxxxxxxxxxxx”

    I also pray you aren’t really using “xxxxxxx” as your password =)

    #232591 Reply

    Did you already download and install MS SQL Server SP3?

    #232598 Reply

    Mork
    Participant

    Yes, I just installed SQL Server SP3a but no dice…Same problem. Cannot connect to database in Database Explorer.

    I’m using MyEclipse 3.8.4.

    I also tried the sourceforge SQL Server drivers with the same problem.

    I still get an error while Database Explorer is trying to log in.

    Just to be sure I took the same connection info and put it into a Java program and connected to the database (and printed out table data) with no problems!

    My connect dialog looks like this (though I’ve tried adding user and password in one string too):

    jdbc:microsoft:sqlserver://localhost:1433;databasename=HibernateDemo

    Then, in the connect dialog in MyEclipse’s database explorer I have the user name and passwords filled in.

    When I try to connect I get the error dialog about problems logging in.

    This is very strange.

    Hope we can figure this out.

    Thanks again in advance.

    — M

    #232599 Reply

    Riyad Kalla
    Member

    It’s actually “databaseName” (capital N), try that.

    Also check this out: http://support.microsoft.com/default.aspx?scid=kb;en-us;313100

    #232608 Reply

    Mork
    Participant

    Thanks for your reply.

    Unfortunately, ‘databaseName’ made no difference. Also, ‘databasename’ worked fine in the Java code I’ve been using to test this problem.

    Thanks for the JDBC link info… But, as I said in an earlier posting, I wrote JDBC code using the same connection string information, on the same machine, that connected fine and printed out table data (I posted that code at the end of this message).

    Could _you_ try to connect to a SQL Server database there and see if you get the same error?

    Try to create a simple database with a single table. I’d really be interested to hear the results. <s>

    In any case, doesn’t MyEclipse “abstract” all the JDBC nastiness anyway, at least when using Database Explorer? Shouldn’t this connection just … “work”?

    I’ve tried adding both the MS drivers and the other drivers you told me to try to the project as external JARs.

    What could possibly be wrong? I also can log in fine using Query Analyzer.

    I’m sure it’s probably simple problem I’m having, but I sure don’t see it.

    I’ve disabled all firewalls, etc.

    Thanks again very much in advance.

    — M

    Here’s my “quick and dirty” JDBC test code that works fine on the same machine:

    —————————————-

    import java.sql.*;

    /**
    *
    *
    *
    */
    public class TestSQLServerConnectivity
    {

    static final String JDBC_DRIVER = “com.microsoft.jdbc.sqlserver.SQLServerDriver”;

    static final String DATABASE_URL = “jdbc:microsoft:sqlserver://localhost:1433;databaseName=HibernateDemo;User=test;password=123456”;

    static ResultSetMetaData metaData;

    // declare Connection and Statement for accessing
    // and querying database
    private static Connection connection;
    private static Statement statement;

    public static void main(String[] args)

    {

    // JDBC driver name and database URL for MySQL

    try
    {

    // load database driver class
    Class.forName(JDBC_DRIVER);

    // establish connection to database
    connection = DriverManager.getConnection(DATABASE_URL);

    // create Statement for querying database
    statement = connection.createStatement();

    String sql = “SELECT * FROM echo_message”;
    ResultSet rs = statement.executeQuery(sql);

    while (rs.next())
    System.out.println(rs.getString(“msg”));

    }

    catch (Exception e)
    {
    e.printStackTrace();
    }

    finally
    {
    connection = null;
    }

    } // main

    }

    #232609 Reply

    Mork
    Participant

    Ok, I finally figured it out.

    It seems that you have to add the JDBC drivers to your “C:\j2sdk1.4.2_06\jre\lib\ext” directory.

    Just adding them to the project doesn’t do anything (that is, when you go into MyEclipse and look at the database explorer drivers installed, you don’t see that the SQL Server is checked until you add the drivers to the “C:\j2sdk1.4.2_06\jre\lib\ext” directory.

    Then all works fine!

    I was trying to figure out how to add these drivers to MyEclipse so I wouldn’t have to worry about the “C:\j2sdk1.4.2_06\jre\lib\ext” directory, but didn’t see how to do this (again, just adding the external Jars to the project didn’t do it).

    — M

    #234740 Reply

    /*
    * ConnectionPool.java
    *
    * Created on 3 de mayo de 2004, 12:08 PM
    */

    package baseDatos;
    import java.sql.*;
    /**
    *
    * @author Administrator
    */
    public class ConnectionPool {

    private Connection pool[];
    private long timeout = 1000;
    private String user, password, url, driver;
    /** Creates a new instance of ConnectionPool */
    public ConnectionPool(int connections) {
    pool = new Connection[connections];
    }

    public void setDriver(String _driver){
    driver = _driver;
    }

    public void setURLConnection(String _url){
    url = _url;
    }

    public void setUser(String _user){
    user = _user;
    }

    public void setPassword(String _password){
    password = _password;
    }

    public void initPool(){
    try{
    Class.forName(driver);//.newInstance();

    for (int i = 0; i < pool.length; i++){
    pool[i] = DriverManager.getConnection(url, user, password);
    }
    }catch (Exception e){e.printStackTrace();

    }

    }

    public synchronized Connection getConnection() {

    Connection con = null;
    try{

    long timeStart = System.currentTimeMillis();

    while((System.currentTimeMillis() – timeStart) < timeout){
    for (int i =0; i < pool.length; i++){
    if (pool[i] != null){
    con = pool[i];
    pool[i] = null;
    break;
    }
    }
    if (con != null) break;
    }

    }catch (Exception e){e.printStackTrace();}
    return con;
    }

    public synchronized void returnConnection(Connection con){
    for (int i =0; i < pool.length; i++){
    if (pool[i] == null){
    pool[i] = con;
    break;
    }
    }
    }

    public void closePool(){
    try{
    for (int i =0; i < pool.length; i++){
    if (pool[i] != null){
    pool[i].close();
    }
    }
    }catch (Exception e){}
    }
    }

    #234741 Reply

    /*
    * Conexion.java
    *
    * Created on 4 de mayo de 2004, 09:24 PM
    */

    package baseDatos;
    import java.sql.*;
    import java.util.*;
    /** Es un ejemplo de JavaDoc
    * @author <B>Jose Adrian Aleman Rojas</B>
    */
    public class BaseDatos {

    Connection c;
    ResultSet registros;
    /** Inicia la base de datos con una Conexion
    * @param _c Conexion con la base de datos
    * @see java.sql.Connection
    */
    public BaseDatos(Connection _c) {
    c = _c;
    }

    /** Ejecuta una consulta, mas especificamente un Select
    * @return <B>True</B> si encontro registros
    * @param _query Consulta
    * @param parametros Es una lista de parametros para la consulta de un {@link
    * java.sql.PreparedStatement}
    * @throws SQLException Lanza una excepcion en caso de algun error de base de datos
    */
    public boolean ejecutarConsulta(String _query, ArrayList _parametros)
    throws SQLException{

    PreparedStatement sentencia = c.prepareStatement(_query);
    int indiceFinal = _parametros.size();
    for (int indice = 0; indice < indiceFinal; indice ++){
    Object parametro = _parametros.get(indice);
    if (parametro instanceof String) {
    sentencia.setString(indice + 1, parametro.toString());
    }
    if (parametro instanceof Integer) {
    sentencia.setInt(indice + 1, ((Integer)parametro).intValue());
    }
    }
    registros = sentencia.executeQuery();

    if (registros != null){
    return true;
    }
    return false;
    }

    /** Ejecuta una consulta de actualizacion
    * @param _query Consulta
    * @param parametros Lista de parametros de la consulta <B>_query</B>
    * @throws SQLException Lanza una excepcion si hay algun error
    * @return Retorna el numero de registros actualizados despues de ejecutar la consukta
    * <B>_query</B>
    */
    public int ejecutarActualizacion(String _query, ArrayList parametros)
    throws SQLException{

    PreparedStatement sentencia = c.prepareStatement(_query);
    int indiceFinal = parametros.size();
    for (int indice = 0; indice < indiceFinal; indice ++){
    Object parametro = parametros.get(indice);
    if (parametro instanceof String) {
    sentencia.setString(indice + 1, parametro.toString());
    }
    if (parametro instanceof Integer) {
    sentencia.setInt(indice + 1, ((Integer)parametro).intValue());
    }
    }
    return sentencia.executeUpdate();
    }

    /** Obtiene el siguiente registro
    * @return <B>True </B> si existe un siguiente registro
    * @throws SQLException Si ocurre una excepcion de base de datos
    */
    public boolean obtenerSiguiente() throws SQLException{
    return registros.next();
    }

    /** Obtiene una hilera
    * @param columna La columna a obtener
    * @return String Retorna una hilera segun el parametro <B>column</B>
    * @throws SQLException Si ocurre algun error
    */
    public String obtenerHilera(int columna) throws SQLException{
    return registros.getString(columna);
    }

    public String obtenerHilera(String columna) throws SQLException{
    return registros.getString(columna);
    }

    public int obtenerEntero(int columna) throws SQLException{
    return registros.getInt(columna);
    }

    public int obtenerEntero(String columna) throws SQLException{
    return registros.getInt(columna);
    }
    }

    #234742 Reply

    /*
    * Created on 31/07/2005
    *
    * TODO To change the template for this generated file go to
    * Window – Preferences – Java – Code Style – Code Templates
    */
    package servlet;

    import java.io.IOException;
    import java.sql.Connection;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.struts.action.ActionServlet;

    import baseDatos.BaseDatos;
    import baseDatos.ConnectionPool;

    /**
    * @author e.chongkan
    *
    * TODO To change the template for this generated type comment go to
    * Window – Preferences – Java – Code Style – Code Templates
    */
    public class MyServlet extends ActionServlet {

    private ConnectionPool pool = new ConnectionPool(5);

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {

    try
    {
    Connection con = pool.getConnection();
    BaseDatos datos = new BaseDatos(con);
    request.setAttribute(“DB”,datos);
    super.doPost(request, response);
    pool.returnConnection(con);
    //con.close();
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }

    public void init() throws ServletException {
    try{
    pool.setDriver(“com.microsoft.jdbc.sqlserver.SQLServerDriver”);
    pool.setURLConnection(“jdbc:microsoft:sqlserver://localhost:1433”);
    pool.setUser(“root”);
    pool.setPassword(“root”);
    pool.initPool();
    super.init();
    } catch (Exception e){
    e.printStackTrace();
    }
    }

    }

    #234745 Reply

    String consulta1 = “Select * from tbl_paises”;
    String consulta2 = “Select * from tbl_nivel_estudios”;
    try{
    BaseDatos DB = (BaseDatos) request.getAttribute(“DB”);
    ArrayList paises = new ArrayList();
    ArrayList estudios = new ArrayList();

    if (request.getSession().getAttribute(“nivel_estudios”) == null) {
    DB.ejecutarConsulta(consulta1, new ArrayList());

    while (DB.obtenerSiguiente()) {
    CargarForm est= new CargarForm();
    est.setId(DB.obtenerEntero(1));
    est.setNivel(DB.obtenerHilera(2));
    estudios.add(est);
    }
    request.getSession().setAttribute(“nivel_estudios”, estudios);
    }

    if (request.getSession().getAttribute(“nacionalidad”) == null) {
    DB.ejecutarConsulta(consulta2, new ArrayList());

    while (DB.obtenerSiguiente()) {
    CargarForm nac = new CargarForm();
    nac.setId_pais(DB.obtenerEntero(1));
    nac.setPais(DB.obtenerHilera(2));
    paises.add(nac);
    }
    request.getSession().setAttribute(“nacionalidad”, paises);
    }
    }
    catch(Exception e){
    e.printStackTrace();
    }
    return mapping.findForward(“registro_candidato”);

Viewing 11 posts - 1 through 11 (of 11 total)
Reply To: Can’t connect to SQL Server Database using Database Explorer

You must be logged in to post in the forum log in