如何从NetBean 7.0.1连接到数据库

如何从NetBean 7.0.1连接到数据库

问题描述:

我正在使用64位Windows并尝试连接到数据库,但它显示以下消息:

I'm using a 64 bit Windows and trying to connect to the database but it shows the following message:

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

有人可以帮助我解决这个问题吗?

Anybody could help me to solve this problem?

请指定您要连接的SQL版本.如果是SQL Server 2008 R2,则以下代码可能会对您有所帮助.这里的mywebsitedb是在MS SQL 2008中创建的数据库的名称,用户名= sa,密码= thatstrue.我创造的.但是首先,如上所述,您必须制作一个数据源,其步骤如下:

Please specify , as to which version of SQL you are trying to connect to. If it's SQL Server 2008 R2, then the following code might help you. Here mywebsitedb is the name of the Database created in MS SQL 2008, with username = sa and password = thatstrue. that I had created. But first, as said above, you have to make one Data Source, for which steps are as follows :

  1. 通过进入控制面板"中的管理工具",打开数据源(ODBC).
  2. 在用户DSN"标签下,单击添加".
  3. 在创建新数据源"窗口中,选择"SQL Server",然后单击完成".
  4. 在为SQL Server创建新数据源的内部,为数据源和描述提供任何名称,服务器名称必须是要使用的名称 在启动MS SQL Management Studio时连接到的对象,以及 单击下一步.
  5. 在新窗口中,选择使用SQL Server身份验证",然后选中连接到SQL Server以获取默认值"复选框 设置",并在相应的位置提供您的用户名和密码 字段,然后单击下一步.
  6. 在新窗口中,选中将默认数据库更改为"复选框,然后从列表中选择数据库,然后单击下一步".
  7. 在新窗口中,单击完成.
  8. ODBC Microsoft SQL Server设置窗口将打开.单击测试数据源,如果一切正常,您将看到消息 说测试已成功完成!".按确定",然后再次按确定".
  1. By going into the Administrative Tools in Control Panel, open Data Sources (ODBC).
  2. Under User DSN tab, click Add.
  3. Inside the Create New Data Source Window Select SQL Server and click Finish.
  4. Inside Create New Data Source to SQL Server, provide any name to the Data Source and description, the Server Name must be the one to which you connect to while launching MS SQL Management Studio and click next.
  5. On the New Window, Select With SQL Server Authentication and check the Checkbox for "Connect to SQL Server to obtain default settings", and provide your username and password in the respective fields and click Next.
  6. On the New Window, check the Checkbox for "Change the default Database to", and Select your database from the list and click Next.
  7. On the New Window click Finish.
  8. ODBC Microsoft SQL Server Setup Window will open. Click Test Data Source, if everything is alright, you will see the Message saying "TEST COMPLETED SUCCESSFULLY!". Press OK and OK again.

您已设置为现在连接到数据库.

You are setUp to connect to your database now.

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    public class EstablishConnection
    {
      public Connection getConnectionObject()
      {
        try
        {      
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
          Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost;database=mywebsitedb;user=sa;password=thatstrue;");

         return con;
        }
        catch(SQLException e)    
        {
          System.err.println("Hello Connecting not possible.");
          e.printStackTrace();
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }

        return null;
      }
    }