用于连接到 Sql Server 并从 Eclipse 运行示例查询的 Java 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19047414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:39:10  来源:igfitidea点击:

Java program to connect to Sql Server and running the sample query From Eclipse

javaeclipsejdbcjdbc-odbc

提问by Venkat

package sqlselection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Sqlselection 
    {
        public static void main(String[] args)
        {
            try
            {
                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

                String userName = "sa";
                String password = "password";
                String url = "jdbc:microsoft:sqlserver://localhost:1433"+";databaseName=AdventureWorks2008R2";
                Connection con = DriverManager.getConnection(url, userName, password);
                Statement s1 = con.createStatement();
                ResultSet rs = s1.executeQuery("SELECT TOP 1 * FROM HumanResources.Employee");
                String[] result = new String[20];
                if(rs!=null){
                    while (rs.next()){
                        for(int i = 0; i <result.length ;i++)
                        {
                            for(int j = 0; j <result.length;j++)
                            {
                                result[j]=rs.getString(i);
                            System.out.println(result[j]);
                        }
                        }
                    }
                }

                //String result = new result[20];

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


}

    enter code here

The Above is my sample program to connect to the Sql server to run the sample select query from eclipse.

以上是我的示例程序,用于连接到 Sql 服务器以从 Eclipse 运行示例选择查询。

I am getting the below error.

我收到以下错误。

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
    at java.net.URLClassLoader.run(Unknown Source)
    at java.net.URLClassLoader.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at sqlselection.Sqlselection.main(Sqlselection.java:13)

i have added the sqljdbc.jar,sqljdbc4.jar to the library. Help to fix this

我已将 sqljdbc.jar,sqljdbc4.jar 添加到库中。帮助解决这个问题

采纳答案by Rajendra

The problem is with Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");this line. The Class qualified name is wrong

问题出在Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");这条线上。类限定名错误

It is sqlserver.jdbcnot jdbc.sqlserver

sqlserver.jdbc不是jdbc.sqlserver

回答by SpringLearner

you forgotten to add the sqlserver.jarin eclipse external library follow the process to add jar files

你忘记sqlserver.jar在 eclipse 中添加外部库了,按照流程添加 jar 文件

  1. Right click on your project.
  2. click buildpath
  3. click configure bulid path
  4. click add external jar and then give the path of jar
  1. 右键单击您的项目。
  2. 单击构建路径
  3. 点击配置bulid路径
  4. 单击添加外部 jar 然后给出 jar 的路径

回答by Aniket Kulkarni

Add sqlserver.jarHere is link

添加sqlserver.jar这里是链接

As the name suggests ClassNotFoundExceptionin Java is a subclass of java.lang.Exceptionand Comes when Java Virtual Machinetries to load a particular class and doesn't found the requested class in classpath.

作为顾名思义ClassNotFoundException是用Java的一个子类java.lang.Exception,当谈到Java虚拟机试图加载特定的类和没有发现的请求的类的类路径。

Another important point about this Exception is that, It is a checked Exceptionand you need to provide explicitly Exception handling while using methods which can possibly throw ClassNotFoundExceptionin java either by using try-catch block or by using throws clause.

关于此异常的另一个重要点是,它是一个已检查的异常,您需要在使用可能ClassNotFoundException通过 try-catch 块或使用 throws 子句在 java 中抛出的方法时提供显式异常处理。

Oracle docs

甲骨文文档

public class ClassNotFoundException
 extends ReflectiveOperationException

Thrown when an application tries to load in a class through its string name using:

当应用程序尝试通过其字符串名称加载类时抛出:

  • The forName method in class Class.
  • The findSystemClass method in class ClassLoader .
  • The loadClass method in class ClassLoader.
  • Class 类中的 forName 方法。
  • ClassLoader 类中的 findSystemClass 方法。
  • ClassLoader 类中的 loadClass 方法。

but no definition for the class with the specified name could be found.

但是找不到具有指定名称的类的定义。

回答by Sajan Chandran

The linkhas the driver for sqlserver, download and add it your eclipsebuildpath.

链接包含驱动程序sqlserver,下载并将其添加到您的eclipse构建路径。

回答by Mogana

Right click your project--->Build path---->configure Build path----> Libraries Tab--->Add External jars--->(Navigate to the location where you have kept the sql driver jar)--->ok

右键单击您的项目--->构建路径---->配置构建路径---->库选项卡--->添加外部jar--->(导航到保存sql驱动程序jar的位置) --->好的

回答by Darshan Shah

Refer the below link.

请参考以下链接。

There are two important changes that you should make

您应该做出两个重要的改变

driver name as "com.microsoft.sqlserver.jdbc.SQLServerDriver"

驱动程序名称为“com.microsoft.sqlserver.jdbc.SQLServerDriver”

& in URL "jdbc:sqlserver://localhost:1433"+";databaseName=AdventureWorks2008R2"

& 在 URL "jdbc:sqlserver://localhost:1433"+";databaseName=AdventureWorks2008R2"

http://www.programcreek.com/2010/05/java-code-for-connecting-ms-sql-server-by-using-sql-server-authentication/

http://www.programcreek.com/2010/05/java-code-for-connecting-ms-sql-server-by-using-sql-server-authentication/

回答by Abdul Moiz

download Microsoft JDBC Driver 4.0 for SQL Server which supports:

下载 Microsoft JDBC Driver 4.0 for SQL Server,它支持:

    SQL Server versions: 2005, 2008, 2008 R2, and 2012.
    JDK version: 5.0 and 6.0.

Run the downloaded program sqljdbc__.exe. It will extract the files into a specified directory (default is Microsoft JDBC Driver 4.0 for SQL Server). You will find two jar files sqljdbc.jar (for JDBC 3.0) and sqljdbc4.jar (for JDBC 4.0), plus some .dll files and HTML help files.

运行下载的程序 sqljdbc__.exe。它将文件解压到指定目录(默认为 Microsoft JDBC Driver 4.0 for SQL Server)。您将找到两个 jar 文件 sqljdbc.jar(用于 JDBC 3.0)和 sqljdbc4.jar(用于 JDBC 4.0),以及一些 .dll 文件和 HTML 帮助文件。

Place the sqljdbc4.jar file under your application's classpath if you are using JDK 4.0 or sqljdbc4.1.jar file if you are using JDK 6.0 or later.

如果您使用的是 JDK 4.0 或 sqljdbc4.1.jar 文件(如果您使用的是 JDK 6.0 或更高版本),请将 sqljdbc4.jar 文件放在应用程序的类路径下。

回答by parwez alam

Just Change the query like this:

只需像这样更改查询:

SELECT TOP 1 * FROM [HumanResources].[Employee]

where Employeeis your table name and HumanResourcesis your Schema name if I am not wrong.

如果我没记错的话,Employee您的表名和HumanResources架构名称在哪里。

Hope your problem will be resolved. :)

希望你的问题能得到解决。:)