简体   繁体   English

从Java连接SQL Server数据库

[英]Connect SQL Server database from Java

I need to Connect the SQL Server 2008 from the Java. 我需要从Java连接SQL Server 2008。

Here is my code: 这是我的代码:

public class Sql {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");


         public static void main(String[] args){
                // Neue DB und los geht's :)
                DB db = new DB();
                db.dbConnect("jdbc:sqlserver://Data Source=500.20.13.1;InitialCatalog=LicenceManagement;UseID=XXXXX;Password=YYYY");
            }
    }

    class DB{

        public void dbConnect(  String db_connect_string, 
                                String db_userid, 
                                String db_password){
            try{
            Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
                Connection conn = DriverManager.getConnection(
                                db_connect_string, 
                            db_userid, 
                            db_password);
                System.out.println( "connected" );
            }
            catch( Exception e ){
                e.printStackTrace();
            }
        }
    };

But the connection isn't established, and I get the following error: 但是未建立连接,并且出现以下错误:

ERR :No suitable driver found for jdbc:sqlserver://Data Source=500.20.13.1;InitialCatalog=LicenceManagement;UseID=XXXXX;Password=YYYY" ERR:找不到适用于jdbc:sqlserver:// Data Source = 500.20.13.1; InitialCatalog = LicenceManagement; UseID = XXXXX; Password = YYYY的驱动程序”

first of all you have to find a driver like @Benedikt Geltenpoth said above (or below). 首先,您必须找到上面(或下面)所说的@Benedikt Geltenpoth这样的驱动程序。

Secondly include your driver in your classpath, after you've download it. 第二,下载驱动程序后,将其包含在类路径中。

Third From Java 1.6 upward, you dont need to register the driver class anymore see (in theorie) here . 第三,从Java 1.6开始,您不再需要注册驱动程序类,请参见(在理论上) 此处 the driver is JDBC type 4 该驱动程序是JDBC类型4

Lastly A simple pattern for your connection would be jdbc:sqlserver://server:port;DatabaseName=dbname plus your url parameters 最后,用于连接的简单模式是jdbc:sqlserver://server:port;DatabaseName=dbname加上您的url参数

    public class Sql {

             public static void main(String[] args){
                    // Neue DB und los geht's :)
                    DB db = new DB();
                    int yourPort = 1433;
                    String initialCatalog = "LicenceManagement";
                    String userId = "userOne";
                    String password= "passwordOne";
db.dbConnect("jdbc:sqlserver://"+500.20.13.1+":"+yourPort+";DatabaseName="+initialCatalog,userId,password);
                }
        }

        class DB{

            public void dbConnect(  String db_connect_string, 
                                    String db_userid, 
                                    String db_password){
                try{
                    Connection conn = DriverManager.getConnection(
                                    db_connect_string, 
                                db_userid, 
                                db_password);
                    System.out.println( "connected" );
                }
                catch( SQLException e ){
                    e.printStackTrace();
                }
            }
        };

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

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