简体   繁体   English

将Java应用程序连接到SQL Server

[英]Connect java app to SQL Server

I'm trying to connect to sqlserver at my java application 我正在尝试在Java应用程序上连接到sqlserver

I have search alot but no one of these method work 我已经搜索了很多,但是这些方法都没有

i use this 我用这个

import java.sql.*;

public class testConnection
{
      public testConnection() throws Exception {  
            // Get connection  
            // DriverManager.registerDriver(net.sourceforge.jtds.jdbc.Driver);  
          Class.forName("net.sourceforge.jtds.jdbc.Driver"); 

            Connection connection = DriverManager.getConnection(  
                    "jdbc:jtds:sqlserver://localhost/testnew", "sa", "123");  
            if (connection != null) {  
                System.out.println();  
                System.out.println("Successfully connected");  
                System.out.println();  
                // Meta data  
                DatabaseMetaData meta = connection.getMetaData();  
                System.out.println("\nDriver Information");  
                System.out.println("Driver Name: " + meta.getDriverName());  
                System.out.println("Driver Version: " + meta.getDriverVersion());  
                System.out.println("\nDatabase Information ");  
                System.out.println("Database Name: "  
                        + meta.getDatabaseProductName());  
                System.out.println("Database Version: "  
                        + meta.getDatabaseProductVersion());  
            }  
        } // Test  

        public static void main(String args[]) throws Exception {  
            testConnection test = new testConnection();  
        }  
}

I'am sure that the user is work but i can't connect to database 我确定该用户正在工作,但是我无法连接到数据库

this is error message 这是错误消息

在此处输入图片说明

Download (for version 1.3.1, eg the current version) jtds-1.3.1-dist.zip from here , unzip it. 此处下载(对于1.3.1版,例如当前版本) jtds-1.3.1-dist.zip ,解压缩。 Add the file jtds-1.3.1.jar to your build path. 将文件jtds-1.3.1.jar添加到您的构建路径。

You might also want to try with SQuirreL SQL Client , which is also free and open-source. 您可能还想尝试SQuirreL SQL Client ,它也是免费的开放源代码。

The class net.sourceforge.jtds.jdbc.Driver does not exist in standard java library like java.sql.Connection and other classes. net.sourceforge.jtds.jdbc.Driver类在标准java库(如java.sql.Connection和其他类)中不存在。

If not already done, a separate jar file that is JDBC driver for MS-SQL Server called JTDS driver should be downloaded and added into the project Build Path. 如果尚未完成,则应下载一个单独的jar文件,该文件是用于MS-SQL Server的JDBC驱动程序,称为JTDS驱动程序,并将其添加到项目“构建路径”中。 By doing this the jar that contains the Driver and relevant classes are made available to the Java Runtime. 通过这样做,包含驱动程序和相关类的jar可供Java Runtime使用。

It does not complain before running the program as a compiler error, because the Class.forName() method takes the Driver class name as a string (or text). 在将程序作为编译器错误运行之前,它不会抱怨,因为Class.forName()方法将Driver类名称作为字符串(或文本)。 For compiler any text is valid, but when run it attempts to load the class with the given name into memory at runtime and it fails, either if it is not a valid class name or a valid class name but is not locatable (not in classpath). 对于编译器,任何文本都是有效的,但是在运行时,它会尝试在运行时将具有给定名称的类加载到内存中,并且失败,无论它不是有效的类名还是有效的类名但都无法定位(不在类路径中) )。

Try this 尝试这个

        SQLServerDataSource sql = new SQLServerDataSource();
        sql.setServerName("Your SQL server name");
        sql.setUser("sa");
        sql.setPassword("your password");
        sql.setPortNumber(1433); // Make sure this port is open
        sql.setDatabaseName("Your database name");

Remember to add "sqljdbc_4.0.jar" to you project's library. 请记住将“ sqljdbc_4.0.jar”添加到项目的库中。

此错误是因为您在类路径中缺少jtds-[version].jar此处获取驱动程序jar表单并将其包含在类路径中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM