com.microsoft.sqlserver.jdbc.SQLServerDriver not found error

前端 未结 8 1857
暖寄归人
暖寄归人 2021-02-13 14:56

I am trying to connect to my SQL Server 2008 database from Java and I\'m having the same problem from this thread.

String userName = \"xxxx\";
String password =          


        
8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-13 15:21

    public static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=dbName";
    public static final String USERNAME = "xxxx";
    public static final String PASSWORD = "xxxx";
    
    /**
     *  This method
        @param args     command line argument
    */
    public static void main(String[] args)
    {
       try
       {
            Connection connection;
            DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());   
            connection = DriverManager.getConnection(MainDriver.URL,MainDriver.USERNAME,
                            MainDriver.PASSWORD);
            String query ="select * from employee";
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(query);
            while(resultSet.next())
            {
                System.out.print("First Name: " + resultSet.getString("first_name"));
                System.out.println("  Last Name: " + resultSet.getString("last_name"));                
            }
       }catch(Exception ex)
       {
            ex.printStackTrace();
       }
    }
    

提交回复
热议问题